cachemdl.go 2.93 KiB
Newer Older
Ajit Jagtap's avatar
Ajit Jagtap committed
//@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"
Ajit Jagtap's avatar
Ajit Jagtap committed
	"github.com/patrickmn/go-cache"
Ajit Jagtap's avatar
Ajit Jagtap committed
)

Ajit Jagtap's avatar
Ajit Jagtap committed
// GCCacheHelper use this to create new cache object
Ajit Jagtap's avatar
Ajit Jagtap committed
// Remember it holds memory
Ajit Jagtap's avatar
Ajit Jagtap committed
type GCCacheHelper struct {
Ajit Jagtap's avatar
Ajit Jagtap committed
	GC         gcache.Cache
	Expiration time.Duration
	MaxEntries int
}

//Setup create new object of GC
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Setup(MaxEntries int, Expiration time.Duration) {
Ajit Jagtap's avatar
Ajit Jagtap committed
	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
Ajit Jagtap's avatar
Ajit Jagtap committed
// func (gchelper *GCCacheHelper) SetupWithCallbackFunction(MaxEntries int, Expiration time.Duration, f func(key interface{}, value interface{}) error) {
Ajit Jagtap's avatar
Ajit Jagtap committed
// 	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
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Set(key string, object interface{}) error {
Ajit Jagtap's avatar
Ajit Jagtap committed
	return gchelper.GC.SetWithExpire(key, object, gchelper.Expiration)
}

//Get object based on key
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Get(key string) (interface{}, error) {
Ajit Jagtap's avatar
Ajit Jagtap committed
	return gchelper.GC.Get(key)
}

// GetAll objects from gc
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) GetAll() map[interface{}]interface{} {
Ajit Jagtap's avatar
Ajit Jagtap committed
	return gchelper.GC.GetALL()
}

// Remove object from GC
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Remove(key string) bool {
Ajit Jagtap's avatar
Ajit Jagtap committed
	return gchelper.GC.Remove(key)
}

//Purge all objects
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Purge() {
Ajit Jagtap's avatar
Ajit Jagtap committed
	gchelper.GC.Purge()
}

// Count all objects
Ajit Jagtap's avatar
Ajit Jagtap committed
func (gchelper *GCCacheHelper) Count() int {
Ajit Jagtap's avatar
Ajit Jagtap committed
	return gchelper.GC.Len()
}
Ajit Jagtap's avatar
Ajit Jagtap committed

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)
}
Ajit Jagtap's avatar
Ajit Jagtap committed
func (fastCacheHelper *FastCacheHelper) Purge() {
	fastCacheHelper.FastCache.Flush()
}