Commit cbbf773f authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Detect cache type at runtime with Type()

parent e551e7b1
Branches
Tags
2 merge requests!173Mep release19032020,!170Add: Functionality to call session manager from bls.
Showing with 17 additions and 24 deletions
......@@ -17,15 +17,12 @@ var (
ErrInvalidCacheType = errors.New("invalid cache type provided")
)
var (
// Set @Setup(), CacheInUse provides current cache in use. We can take decision to perform cache specific operations with this.
CacheInUse int
// Set @setup(), if true, cache is already initialized
initialized bool
)
// Cacher provides access to underlying cache, make sure all caches implement these methods.
//
// The return types of data can be different. Ex. In case of redis cache it is `string`. The caller needs to handle this with the help of Type() method.
//
// Ex.
// (Cacher{}).Type() == TypeRedisCache { // your implementation }
type Cacher interface {
// SET
Set(key string, val interface{})
......@@ -42,6 +39,8 @@ type Cacher interface {
// GetItemsCount
GetItemsCount() int
Type() int
}
// GetCacheInstance returns a cache instance, panics if invalid cache type is provided
......
......@@ -23,7 +23,6 @@ import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/go-redis/redis"
"github.com/tidwall/gjson"
)
const (
......@@ -54,10 +53,6 @@ type RedisCache struct {
// Setup initializes redis cache for application. Must be called only once.
func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Duration) {
if initialized {
loggermdl.LogError("cache instance is already initialized")
return
}
if rc == nil {
rc = new(RedisCache)
......@@ -90,8 +85,6 @@ func (rc *RedisCache) Setup(addr, password, prefix string, db int, exp time.Dura
rc.addPrefix = true
}
CacheInUse = TypeRedisCache
initialized = true
}
// Set marshalls provided value and stores against provided key. Errors will be logged to initialized logger.
......@@ -143,11 +136,11 @@ func (rc *RedisCache) Get(key string) (interface{}, bool) {
// Get returns error if key is not present.
val, err := rc.cli.Get(rc.key(key)).Result()
if err != nil {
loggermdl.LogError("error getting key ", key, " error: ", err)
loggermdl.LogError("error getting key", key, "from redis cache with error:", err)
return nil, false
}
return gjson.Parse(val).Value(), true
return val, true
}
// Delete -
......@@ -210,3 +203,7 @@ func (rc *RedisCache) key(key string) string {
}
return key
}
func (rc *RedisCache) Type() int {
return TypeRedisCache
}
......@@ -7,7 +7,6 @@ package cachemdl
import (
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/patrickmn/go-cache"
)
......@@ -22,17 +21,11 @@ type FastCacheHelper struct {
// Setup initializes fastcache cache for application. Must be called only once.
func (fastCacheHelper *FastCacheHelper) Setup(maxEntries int, expiration time.Duration, cleanupTime time.Duration) {
if initialized {
loggermdl.LogError("cache instance is already initialized")
return
}
fastCacheHelper.MaxEntries = maxEntries
fastCacheHelper.Expiration = expiration
fastCacheHelper.FastCache = cache.New(fastCacheHelper.Expiration, fastCacheHelper.CleanupTime)
CacheInUse = TypeFastCache
initialized = true
}
// Get -
......@@ -74,3 +67,7 @@ func (fastCacheHelper *FastCacheHelper) Delete(key string) {
func (fastCacheHelper *FastCacheHelper) GetItemsCount() int {
return fastCacheHelper.FastCache.ItemCount()
}
func (fh *FastCacheHelper) Type() int {
return TypeFastCache
}
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