Commit 01695c31 authored by Ajit Jagtap's avatar Ajit Jagtap
Browse files

Merge branch 'ab_Add_GetAllCacheItems' into 'devbranch'

Add: Enable users to view data present in session manager

Closes MKCLOS/coreosv3/corestudio#546

See merge request !206
parents d5ddfe0f b33c845e
Branches
Tags
2 merge requests!210Staging mepdeployment05072020,!206Add: Enable users to view data present in session manager
Showing with 161 additions and 23 deletions
......@@ -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)
......
......@@ -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
}
......@@ -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)
}
})
}
}
......@@ -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
}
//@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)
}
})
}
}
......@@ -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) {
......
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