diff --git a/cachemdl/cache.go b/cachemdl/cache.go index e28b970afebca584ff947ed18f899729179ab298..01452d9ec7266b7a53349a6430c784d6b44958f8 100644 --- a/cachemdl/cache.go +++ b/cachemdl/cache.go @@ -25,7 +25,7 @@ type Cacher interface { // GET Get(key string) (interface{}, bool) - // GetAll() map[string]interface{} + GetAll() map[string]interface{} // DELETE Delete(key string) diff --git a/cachemdl/cache_redis.go b/cachemdl/cache_redis.go index 0f532229eba3109e59aa94c0db6479bf8759f656..a516b36c371ae9658ecc54be65706b0235d04e05 100644 --- a/cachemdl/cache_redis.go +++ b/cachemdl/cache_redis.go @@ -29,11 +29,6 @@ import ( const ( noExp time.Duration = 0 - // NotOK refers to a unsuccessfull operations - NotOK int64 = 0 - // OK refers to a successfull operations - OK int64 = 1 - keySplitter = ":" ) @@ -198,17 +193,8 @@ func (rc *RedisCache) SetNoExpiration(key string, val interface{}) { rc.cli.Set(rc.key(key), ba, noExp) } -// Get returns data against provided key. The final result is parsed with gjson. Returns false if not present. +// Get returns data against provided key. Returns false if not present. func (rc *RedisCache) Get(key string) (interface{}, bool) { - // exists, err := rc.cli.Exists(key).Result() - // if err != nil { - // loggermdl.LogError("error checking key ", key, " error: ", err) - // return nil, false - // } - - // if exists == NotOK { - // return nil, false - // } // Get returns error if key is not present. val, err := rc.cli.Get(rc.key(key)).Result() @@ -227,13 +213,13 @@ func (rc *RedisCache) Delete(key string) { // GetItemsCount - func (rc *RedisCache) GetItemsCount() int { - pattern := "*" - keys, err := rc.cli.Keys(pattern).Result() - if err != nil { - loggermdl.LogError("error getting item count for ", pattern, " error: ", err) - return 0 - } - return len(keys) + // pattern := rc.Prefix + "*" + // keys, err := rc.cli.Keys(pattern).Result() + // if err != nil { + // loggermdl.LogError("error getting item count for ", pattern, " error: ", err) + // return 0 + // } + return len(rc.keys()) } func (rc *RedisCache) flushDB() (string, error) { @@ -281,6 +267,47 @@ func (rc *RedisCache) key(key string) string { return key } +func (rc *RedisCache) actualKey(key string) string { + if rc.addPrefix { + return strings.TrimPrefix(key, rc.keyStr) + } + return key +} + func (rc *RedisCache) Type() int { return TypeRedisCache } + +// GetAll returns all keys with values present in redis server. Excludes the keys which does not have specified prefix. If prefix is empty, then returns all keys. +// +// **This is not intended for production use. May hamper performance** +func (rc *RedisCache) GetAll() map[string]interface{} { + keys := rc.keys() + + result := make(map[string]interface{}, len(keys)) + + for i := range keys { + ba, err := rc.cli.Get(keys[i]).Bytes() + if err != nil { + loggermdl.LogError("error getting key", keys[i], "from redis cache with error:", err) + continue + } + + var val interface{} + _ = json.Unmarshal(ba, &val) + + result[rc.actualKey(keys[i])] = val + } + + return result +} + +// GetItemsCount - +func (rc *RedisCache) keys() []string { + pattern := rc.Prefix + "*" + keys, err := rc.cli.Keys(pattern).Result() + if err != nil { + loggermdl.LogError("error getting item count for ", pattern, " error: ", err) + } + return keys +} diff --git a/cachemdl/cache_redis_test.go b/cachemdl/cache_redis_test.go index 7f6c80136c3cf56e3def86f4f57033359ebc5e38..5be7d5bb37f0b54c6d953adf28e518fd84fd8ebf 100644 --- a/cachemdl/cache_redis_test.go +++ b/cachemdl/cache_redis_test.go @@ -405,3 +405,51 @@ func BenchmarkMarshalWithTypeCheckStruct(b *testing.B) { _, _ = marshalWithTypeCheck(s) } } + +func TestRedisCache_GetAll(t *testing.T) { + tests := []struct { + name string + rc *RedisCache + want map[string]interface{} + init func(rc *RedisCache) + }{ + { + name: "Get All Items", + rc: &RedisCache{}, + want: map[string]interface{}{ + "a": 1.24, + "b": 1.25, + }, + init: func(rc *RedisCache) { + rc.Setup("127.0.0.1:6379", "", "tests", 0, time.Second*60) + rc.flushDB() + + rc.Set("a", 1.24) + rc.Set("b", 1.25) + }, + }, + { + name: "Get All Items without prefix", + rc: &RedisCache{}, + want: map[string]interface{}{ + "a": 5.24, + "b": 5.25, + }, + init: func(rc *RedisCache) { + rc.Setup("127.0.0.1:6379", "", "", 0, time.Second*60) + rc.flushDB() + + rc.Set("a", 5.24) + rc.Set("b", 5.25) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.init(tt.rc) + if got := tt.rc.GetAll(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RedisCache.GetAll() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/cachemdl/cachemdl.go b/cachemdl/cachemdl.go index f2eb3f89a8c41d47548655faa127333a6cee4c42..6d7b7e5e65c36bbd066e876969d7fabf2f776840 100755 --- a/cachemdl/cachemdl.go +++ b/cachemdl/cachemdl.go @@ -103,3 +103,15 @@ func (fastCacheHelper *FastCacheHelper) GetItemsCount() int { func (fh *FastCacheHelper) Type() int { return TypeFastCache } + +// GetAll returns all keys with values present in memory. **This is not intended for production use. May hamper performance** +func (fastCacheHelper *FastCacheHelper) GetAll() map[string]interface{} { + items := fastCacheHelper.FastCache.Items() + + result := make(map[string]interface{}, len(items)) + for k, v := range items { + result[k] = v.Object + } + + return result +} diff --git a/cachemdl/cachemdl_test.go b/cachemdl/cachemdl_test.go index 5a815eba95c699ef130136dc653f4498495e0225..de9d36173da7e3e663106b93e2edd0b9942a892e 100755 --- a/cachemdl/cachemdl_test.go +++ b/cachemdl/cachemdl_test.go @@ -1,7 +1,14 @@ //@author Ajit Jagtap + //@version Thu Jul 05 2018 06:11:54 GMT+0530 (IST) + package cachemdl +import ( + "reflect" + "testing" +) + // import ( // "testing" // "time" @@ -39,3 +46,42 @@ package cachemdl // cnt = ch.Count() // assert.Zero(t, cnt, "After Purge Count should be zero") // } + +func TestFastCacheHelper_GetAll(t *testing.T) { + tests := []struct { + name string + fastCacheHelper *FastCacheHelper + want map[string]interface{} + init func(fs *FastCacheHelper) + }{ + { + name: "Get all items Success", + fastCacheHelper: &FastCacheHelper{}, + want: map[string]interface{}{ + "a": 1, + "b": 2, + }, + init: func(fs *FastCacheHelper) { + fs.Setup(2, 0, 0) + fs.Set("a", 1) + fs.Set("b", 2) + }, + }, + { + name: "Get all items Empty", + fastCacheHelper: &FastCacheHelper{}, + want: map[string]interface{}{}, + init: func(fs *FastCacheHelper) { + fs.Setup(2, 0, 0) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.init(tt.fastCacheHelper) + if got := tt.fastCacheHelper.GetAll(); !reflect.DeepEqual(got, tt.want) { + t.Errorf("FastCacheHelper.GetAll() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/sessionmanagermdl/sessionmanager.go b/sessionmanagermdl/sessionmanager.go index 2a642d3d0f4b771f6cc4a0ef39fb87a0d789b088..f023f97a69486f4c58c893e8d88803cbbb6889ab 100644 --- a/sessionmanagermdl/sessionmanager.go +++ b/sessionmanagermdl/sessionmanager.go @@ -111,6 +111,11 @@ func Retrieve(key string) (Entry, error) { } } +// RetrieveAll returns all entries present in memory. **Not for production use. May add performance costs** +func RetrieveAll() map[string]interface{} { + return store.GetAll() +} + // RetrieveAndExtend returns the entry and extends the entry expiration by provided `SECONDS`, only if remaining time < extendBy. // If extendBy < 0, it is same as Retrieve function. func RetrieveAndExtend(key string, extendBy int64) (Entry, error) {