diff --git a/rokey/cache.go b/rokey/cache.go new file mode 100644 index 00000000..25a8257e --- /dev/null +++ b/rokey/cache.go @@ -0,0 +1,70 @@ +package rokey + +import ( + "fmt" + "sync" + "time" + + "github.com/pritunl/mongo-go-driver/bson/primitive" +) + +var ( + cache = map[primitive.ObjectID]*Rokey{} + cacheLock = sync.RWMutex{} + cacheTime = map[string]*Rokey{} + cacheTimeLock = sync.RWMutex{} +) + +func GetCache(typ string, timeblock time.Time) *Rokey { + cacheTimeLock.RLock() + rkey := cacheTime[fmt.Sprintf("%s-%d", typ, timeblock.Unix())] + cacheTimeLock.RUnlock() + if rkey != nil && rkey.Type == typ { + return rkey + } + return nil +} + +func GetCacheId(typ string, rkeyId primitive.ObjectID) *Rokey { + cacheLock.RLock() + rkey := cache[rkeyId] + cacheLock.RUnlock() + if rkey != nil && rkey.Type == typ { + return rkey + } + return nil +} + +func PutCache(rkey *Rokey) { + cacheLock.Lock() + cache[rkey.Id] = rkey + cacheLock.Unlock() + cacheTimeLock.Lock() + cacheTime[fmt.Sprintf("%s-%d", rkey.Type, rkey.Timeblock.Unix())] = rkey + cacheTimeLock.Unlock() +} + +func CleanCache() { + cacheLock.Lock() + for key, rkey := range cache { + if time.Since(rkey.Timestamp) >= 721*time.Hour { + delete(cache, key) + } + } + cacheLock.Unlock() + + cacheTimeLock.Lock() + for key, rkey := range cacheTime { + if time.Since(rkey.Timestamp) >= 721*time.Hour { + delete(cacheTime, key) + } + } + cacheTimeLock.Unlock() +} + +func init() { + go func() { + time.Sleep(1 * time.Hour) + CleanCache() + }() +}