Commit 1d49feaa authored by Sandeep S. Shewalkar's avatar Sandeep S. Shewalkar
Browse files

Update cacheHelper.go

parent ce0b32b5
Branches
Tags
3 merge requests!6Stagingbranch,!5Ss dev branch,!4Ss dev branch
Showing with 63 additions and 63 deletions
package coreos package coreos
import ( import (
"fmt" "fmt"
"time" "time"
"github.com/allegro/bigcache" "github.com/allegro/bigcache"
) )
const ( const (
BIGCACHEShards=8 BIGCACHEShards=8
BIGCACHEMaxEntrySize=1024 BIGCACHEMaxEntrySize=1024
BIGCACHEVerbose=true BIGCACHEVerbose=true
BIGCACHEHardMaxCacheSize=0 BIGCACHEHardMaxCacheSize=0
BIGCACHEMaxEntriesInWindow=1000 10 60 BIGCACHEMaxEntriesInWindow=1000 10 60
BIGCACHELifeWindow=2 BIGCACHELifeWindow=2
) )
var bigcacheConfig = bigcache.Config{ var bigcacheConfig = bigcache.Config{
// number of shards (must be a power of 2) // number of shards (must be a power of 2)
// Shards: 4096, // Shards: 4096,
Shards: BIGCACHEShards, Shards: BIGCACHEShards,
// time after which entry can be evicted // time after which entry can be evicted
LifeWindow: BIGCACHELifeWindow * time.Hour, LifeWindow: BIGCACHELifeWindow * time.Hour,
// rps * lifeWindow, used only in initial memory allocation // rps * lifeWindow, used only in initial memory allocation
MaxEntriesInWindow: 1000 * 10 * 60, MaxEntriesInWindow: 1000 * 10 * 60,
// MaxEntriesInWindow: 10, // MaxEntriesInWindow: 10,
// max entry size in bytes, used only in initial memory allocation // max entry size in bytes, used only in initial memory allocation
MaxEntrySize: BIGCACHEMaxEntrySize, MaxEntrySize: BIGCACHEMaxEntrySize,
// prints information about additional memory allocation // prints information about additional memory allocation
Verbose: BIGCACHEVerbose, Verbose: BIGCACHEVerbose,
// cache will not allocate more memory than this limit, value in MB // cache will not allocate more memory than this limit, value in MB
// if value is reached then the oldest entries can be overridden for the new ones // if value is reached then the oldest entries can be overridden for the new ones
// 0 value means no size limit // 0 value means no size limit
HardMaxCacheSize: BIGCACHEHardMaxCacheSize, HardMaxCacheSize: BIGCACHEHardMaxCacheSize,
// callback fired when the oldest entry is removed because of its // callback fired when the oldest entry is removed because of its
// expiration time or no space left for the new entry. Default value is nil which // expiration time or no space left for the new entry. Default value is nil which
// means no callback and it prevents from unwrapping the oldest entry. // means no callback and it prevents from unwrapping the oldest entry.
OnRemove: nil, OnRemove: nil,
} }
var cache, initErr = bigcache.NewBigCache(bigcacheConfig) var cache, initErr = bigcache.NewBigCache(bigcacheConfig)
//GetValue GetValue //GetValue GetValue
func GetValue(key string) ([]byte, error) { func GetValue(key string) ([]byte, error) {
return cache.Get(key) return cache.Get(key)
} }
//SetValue SetValue //SetValue SetValue
func SetValue(key string, value []byte) { func SetValue(key string, value []byte) {
cache.Set(key, value) cache.Set(key, value)
} }
//GetLength GetLength //GetLength GetLength
func GetLength() int { func GetLength() int {
return cache.Len() return cache.Len()
} }
//Callback function executed when cache element is removed. //Callback function executed when cache element is removed.
//Executed only when onRemove of cache config poting to this function //Executed only when onRemove of cache config poting to this function
func onRemove(key string, entry []byte) { func onRemove(key string, entry []byte) {
fmt.Println(key + " removed at " + time.Now().String()) fmt.Println(key + " removed at " + time.Now().String())
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment