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
58ae9729
Commit
58ae9729
authored
4 years ago
by
Akshay Bharambe
Browse files
Options
Downloads
Patches
Plain Diff
Refactor: Redis cache
parent
4e0bc266
2 merge requests
!210
Staging mepdeployment05072020
,
!206
Add: Enable users to view data present in session manager
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cachemdl/cache_redis.go
+28
-30
cachemdl/cache_redis.go
cachemdl/cache_redis_test.go
+15
-0
cachemdl/cache_redis_test.go
with
43 additions
and
30 deletions
cachemdl/cache_redis.go
+
28
−
30
View file @
58ae9729
...
...
@@ -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
}
This diff is collapsed.
Click to expand it.
cachemdl/cache_redis_test.go
+
15
−
0
View file @
58ae9729
...
...
@@ -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
)
{
...
...
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