Commit 4e0bc266 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: GetAll for redis cache

parent aae0de39
Branches
Tags
2 merge requests!210Staging mepdeployment05072020,!206Add: Enable users to view data present in session manager
Showing with 62 additions and 0 deletions
......@@ -284,3 +284,32 @@ func (rc *RedisCache) key(key string) string {
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{} {
pattern := rc.Prefix + "*"
keys, err := rc.cli.Keys(pattern).Result()
if err != nil {
loggermdl.LogError("error getting keys for ", pattern, " error: ", err)
return nil
}
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)
// accessing [1] won't panic as we have all keys with matching regex
result[strings.Split(keys[i], keySplitter)[1]] = val
}
return result
}
......@@ -405,3 +405,36 @@ 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)
},
},
}
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)
}
})
}
}
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