//@author Ajit Jagtap //@version Thu Jul 05 2018 06:12:07 GMT+0530 (IST) // Package cachemdl will help cache object into memory. It Uses LRU algo package cachemdl import ( "time" "github.com/bluele/gcache" "github.com/patrickmn/go-cache" ) // GCCacheHelper use this to create new cache object // Remember it holds memory type GCCacheHelper struct { GC gcache.Cache Expiration time.Duration MaxEntries int } //Setup create new object of GC func (gchelper *GCCacheHelper) Setup(MaxEntries int, Expiration time.Duration) { gchelper.MaxEntries = MaxEntries gchelper.Expiration = Expiration gchelper.GC = gcache.New(MaxEntries). LFU(). Build() } //TODO: Check if you have SetUp value // //Setup create new object of GC // func (gchelper *GCCacheHelper) SetupWithCallbackFunction(MaxEntries int, Expiration time.Duration, f func(key interface{}, value interface{}) error) { // gchelper.MaxEntries = MaxEntries // gchelper.Expiration = Expiration // gchelper.GC = gcache.New(MaxEntries). // LFU(). // EvictedFunc(func(key, value interface{}) { // f(key, value) // }). // Build() // } //New Add new Key and value func (gchelper *GCCacheHelper) Set(key string, object interface{}) error { return gchelper.GC.SetWithExpire(key, object, gchelper.Expiration) } //Get object based on key func (gchelper *GCCacheHelper) Get(key string) (interface{}, error) { return gchelper.GC.Get(key) } // GetAll objects from gc func (gchelper *GCCacheHelper) GetAll() map[interface{}]interface{} { return gchelper.GC.GetALL() } // Remove object from GC func (gchelper *GCCacheHelper) Remove(key string) bool { return gchelper.GC.Remove(key) } //Purge all objects func (gchelper *GCCacheHelper) Purge() { gchelper.GC.Purge() } // Count all objects func (gchelper *GCCacheHelper) Count() int { return gchelper.GC.Len() } type FastCacheHelper struct { FastCache *cache.Cache Expiration time.Duration CleanupTime time.Duration MaxEntries int } //Setup create new object of GC func (fastCacheHelper *FastCacheHelper) Setup(maxEntries int, expiration time.Duration, cleanupTime time.Duration) { fastCacheHelper.MaxEntries = maxEntries fastCacheHelper.Expiration = expiration fastCacheHelper.FastCache = cache.New(fastCacheHelper.Expiration, fastCacheHelper.CleanupTime) } func (fastCacheHelper *FastCacheHelper) Get(key string) (interface{}, bool) { return fastCacheHelper.FastCache.Get(key) } func (fastCacheHelper *FastCacheHelper) SetNoExpiration(key string, object interface{}) { fastCacheHelper.FastCache.Set(key, object, cache.NoExpiration) } func (fastCacheHelper *FastCacheHelper) Set(key string, object interface{}) { fastCacheHelper.FastCache.Set(key, object, cache.DefaultExpiration) } func (fastCacheHelper *FastCacheHelper) SetWithExpiration(key string, object interface{}, duration time.Duration) { fastCacheHelper.FastCache.Set(key, object, duration) } func (fastCacheHelper *FastCacheHelper) Purge() { fastCacheHelper.FastCache.Flush() }