cachemdl.go 1.83 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"

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
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)
}

Ajit Jagtap's avatar
Ajit Jagtap committed
func (fastCacheHelper *FastCacheHelper) Get(key string) (interface{}, bool) {
	return fastCacheHelper.FastCache.Get(key)
}

func (fastCacheHelper *FastCacheHelper) GetItems() map[string]cache.Item {
	return fastCacheHelper.FastCache.Items()
}

Ajit Jagtap's avatar
Ajit Jagtap committed
func (fastCacheHelper *FastCacheHelper) SetNoExpiration(key string, object interface{}) {
	fastCacheHelper.FastCache.Set(key, object, cache.NoExpiration)
}
Ajit Jagtap's avatar
Ajit Jagtap committed
func (fastCacheHelper *FastCacheHelper) Set(key string, object interface{}) {
	fastCacheHelper.FastCache.Set(key, object, cache.DefaultExpiration)
}

// SetWithExpiration -
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()
}
Roshan Patil's avatar
Roshan Patil committed
func (fastCacheHelper *FastCacheHelper) Delete(key string) {
	fastCacheHelper.FastCache.Delete(key)
}

// GetItemsCount : Number of items in the cache
func (fastCacheHelper *FastCacheHelper) GetItemsCount() int {
	return fastCacheHelper.FastCache.ItemCount()
}