Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
MKCLOS
Core Development Platform
corepkgv2
Commits
48d8b25e
Commit
48d8b25e
authored
5 years ago
by
Akshay Bharambe
Browse files
Options
Downloads
Patches
Plain Diff
Add: Purge redis cache
1. Add: Purge redis cache
parent
97cab550
Branches
Branches containing commit
Tags
Tags containing commit
2 merge requests
!134
Mep release271219
,
!130
Add: Redis as a cache to support grid mode
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
cachemdl/cache.go
+1
-1
cachemdl/cache.go
cachemdl/cache_redis.go
+15
-3
cachemdl/cache_redis.go
cachemdl/cache_redis_test.go
+85
-1
cachemdl/cache_redis_test.go
with
101 additions
and
5 deletions
cachemdl/cache.go
+
1
−
1
View file @
48d8b25e
...
...
@@ -30,7 +30,7 @@ type Cacher interface {
// DELETE
Delete
(
key
string
)
//
Purge()
Purge
()
// GetItemsCount
GetItemsCount
()
int
...
...
This diff is collapsed.
Click to expand it.
cachemdl/cache_redis.go
+
15
−
3
View file @
48d8b25e
...
...
@@ -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
.
M
arshal
(
val
)
ba
,
err
:=
m
arshal
(
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
.
M
arshal
(
val
)
ba
,
err
:=
m
arshal
(
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
.
M
arshal
(
val
)
ba
,
err
:=
m
arshal
(
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
)
}
This diff is collapsed.
Click to expand it.
cachemdl/cache_redis_test.go
+
85
−
1
View file @
48d8b25e
...
...
@@ -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
)
})
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets