Commit 58ae9729 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Refactor: Redis cache

parent 4e0bc266
Branches
Tags
2 merge requests!210Staging mepdeployment05072020,!206Add: Enable users to view data present in session manager
Showing with 43 additions and 30 deletions
......@@ -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 := 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(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,13 @@ 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
}
......@@ -289,12 +282,7 @@ func (rc *RedisCache) Type() int {
//
// **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
}
keys := rc.keys()
result := make(map[string]interface{}, len(keys))
......@@ -308,8 +296,18 @@ func (rc *RedisCache) GetAll() map[string]interface{} {
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
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
}
......@@ -428,6 +428,21 @@ func TestRedisCache_GetAll(t *testing.T) {
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) {
......
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