Commit 81f62b46 authored by Akshay Bharambe's avatar Akshay Bharambe
Browse files

Add: Redis as a cache support for grid mode.

1. Add: Redis as a cache support for grid mode. Redis implements cacher interface.
parent 48262fe4
Branches
Tags
2 merge requests!134Mep release271219,!130Add: Redis as a cache to support grid mode
Showing with 132 additions and 0 deletions
package cachemdl
/*
Provides cache access and manipulation methods for rdis server
Implements cacher interface.
Official docs -
1. redis client - https://github.com/go-redis/redis
2. redis server - https://redis.io/
Note - corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl must be initialized before use
*/
import (
"encoding/json"
"time"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"github.com/go-redis/redis"
"github.com/tidwall/gjson"
)
const (
noExp time.Duration = 0
// NotOK refers to a unsuccessfull operations
NotOK int64 = 0
// OK refers to a successfull operations
OK int64 = 1
)
// RedisCache -
type RedisCache struct {
cli *redis.Client // represents redis client
opt *redis.Options //
Addr string // redis server address, default "127.0.0.1:6379"
DB int // redis DB on provided server, default 0
Password string //
}
// Setup -
func (rc *RedisCache) Setup(addr, password string, db int) {
opt := redis.Options{
Addr: addr,
Password: password,
DB: db,
}
rc.opt = &opt
rc.cli = redis.NewClient(&opt)
}
// 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)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
}
rc.cli.Set(key, ba, noExp)
}
// 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)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
}
rc.cli.Set(key, ba, exp)
}
// SetNoExpiration marshalls provided value and stores against provided key.
// Same as Set method.
// Errors will be logged to initialized logger.
func (rc *RedisCache) SetNoExpiration(key string, val interface{}) {
ba, err := json.Marshal(val)
if err != nil {
loggermdl.LogError("error setting key ", key, " error: ", err)
return
}
rc.cli.Set(key, ba, noExp)
}
// Get returns data against provided key. The final result is parsed with gjson. 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(key).Result()
if err != nil {
loggermdl.LogError("error getting key ", key, " error: ", err)
return nil, false
}
return gjson.Parse(val).Value(), true
}
// Delete -
func (rc *RedisCache) Delete(key string) {
rc.cli.Del(key).Result()
}
// 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)
}
func (rc *RedisCache) flushDB() (string, error) {
return rc.cli.FlushDB().Result()
}
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