Commit 48d8b25e authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Purge redis cache

1. Add: Purge redis cache
parent 97cab550
Branches
Tags
2 merge requests!134Mep release271219,!130Add: Redis as a cache to support grid mode
Showing with 101 additions and 5 deletions
......@@ -30,7 +30,7 @@ type Cacher interface {
// DELETE
Delete(key string)
// Purge()
Purge()
// GetItemsCount
GetItemsCount() int
......
......@@ -64,7 +64,7 @@ func (rc *RedisCache) Setup(addr, password string, db int) {
// Set marshalls provided value and stores against provided key. Errors will be logged to initialized logger.
func (rc *RedisCache) Set(key string, val interface{}) {
ba, err := json.Marshal(val)
ba, err := marshal(val)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
......@@ -75,7 +75,7 @@ func (rc *RedisCache) Set(key string, val interface{}) {
// SetWithExpiration marshalls provided value and stores against provided key for given duration. Errors will be logged to initialized logger.
func (rc *RedisCache) SetWithExpiration(key string, val interface{}, exp time.Duration) {
ba, err := json.Marshal(val)
ba, err := marshal(val)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
......@@ -88,7 +88,7 @@ func (rc *RedisCache) SetWithExpiration(key string, val interface{}, exp time.Du
// Same as Set method.
// Errors will be logged to initialized logger.
func (rc *RedisCache) SetNoExpiration(key string, val interface{}) {
ba, err := json.Marshal(val)
ba, err := marshal(val)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
......@@ -138,3 +138,15 @@ func (rc *RedisCache) GetItemsCount() int {
func (rc *RedisCache) flushDB() (string, error) {
return rc.cli.FlushDB().Result()
}
// Purge deletes for current redis db
func (rc *RedisCache) Purge() {
_, err := rc.flushDB()
if err != nil {
loggermdl.LogError("error purging redis cache for db ", rc.Addr, "/", rc.DB, " error: ", err)
}
}
func marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
......@@ -7,7 +7,8 @@ import (
)
var (
res = "TestVal"
res = "TestVal"
failMarshal = func() {}
)
func setup(rc *RedisCache) {
......@@ -21,6 +22,9 @@ func TestRedisCache_Set(t *testing.T) {
testName1 = "SET"
key1 = "test_set"
val1 = "test_set_val"
testName2 = "SET_MarshalErr"
key2 = "test_set_err"
val2 = failMarshal
)
setup(rc)
......@@ -38,6 +42,11 @@ func TestRedisCache_Set(t *testing.T) {
rc: rc,
args: args{key: key1, val: val1},
},
{
name: testName2,
rc: rc,
args: args{key: key2, val: val2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -62,6 +71,9 @@ func TestRedisCache_SetWithExpiration(t *testing.T) {
testName1 = "SET_EXP"
key1 = "test_set_exp"
val1 = "test_set_exp_val"
testName2 = "SET_EXP_MarshalErr"
key2 = "test_set_exp_err"
val2 = failMarshal
)
setup(rc)
......@@ -80,6 +92,11 @@ func TestRedisCache_SetWithExpiration(t *testing.T) {
rc: rc,
args: args{key: key1, val: val1, exp: time.Second * 2},
},
{
name: testName2,
rc: rc,
args: args{key: key2, val: val2, exp: time.Second * 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -104,6 +121,9 @@ func TestRedisCache_SetNoExpiration(t *testing.T) {
testName1 = "SET_NOEXP"
key1 = "test_set_noexp"
val1 = "test_set_noexp_val"
testName2 = "SET_NOEXP_MarshalErr"
key2 = "test_set_noexp_err"
val2 = failMarshal
)
setup(rc)
......@@ -121,6 +141,11 @@ func TestRedisCache_SetNoExpiration(t *testing.T) {
rc: rc,
args: args{key: key1, val: val1},
},
{
name: testName2,
rc: rc,
args: args{key: key2, val: val2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -252,3 +277,62 @@ func TestRedisCache_GetItemsCount(t *testing.T) {
})
}
}
func TestRedisCache_Purge(t *testing.T) {
var (
rc = &RedisCache{}
testName1 = "PURGE"
key1 = "test_purge"
val1 = "test_purge_val"
)
setup(rc)
rc.Set(key1, val1)
tests := []struct {
name string
rc *RedisCache
}{
// TODO: Add test cases.
{
name: testName1,
rc: rc,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.rc.Purge()
})
}
if _, ok := rc.Get(key1); ok {
t.Error("value not deleted for", key1)
}
}
func TestRedisCache_Setup(t *testing.T) {
var (
rc *RedisCache = nil
testName1 = "SETUP"
)
type args struct {
addr string
password string
db int
}
tests := []struct {
name string
rc *RedisCache
args args
}{
// TODO: Add test cases.
{
name: testName1,
rc: rc,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.rc.Setup(tt.args.addr, tt.args.password, tt.args.db)
})
}
}
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