59 lines
1.0 KiB
Go
59 lines
1.0 KiB
Go
|
package cache
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/Secured-Finance/dione/config"
|
||
|
"github.com/fxamacker/cbor/v2"
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
)
|
||
|
|
||
|
type RedisCache struct {
|
||
|
Client *redis.Client
|
||
|
ctx context.Context
|
||
|
}
|
||
|
|
||
|
func NewRedisCache(config *config.Config) *RedisCache {
|
||
|
client := redis.NewClient(&redis.Options{
|
||
|
Addr: config.Redis.Addr,
|
||
|
Password: config.Redis.Password,
|
||
|
DB: config.Redis.DB,
|
||
|
})
|
||
|
|
||
|
return &RedisCache{
|
||
|
Client: client,
|
||
|
ctx: context.Background(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (rc *RedisCache) Store(key string, value interface{}) error {
|
||
|
mRes, err := cbor.Marshal(value)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
rc.Client.Set(rc.ctx, key, mRes, 0)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (rc *RedisCache) Get(key string, value interface{}) error {
|
||
|
data, err := rc.Client.Get(rc.ctx, key).Bytes()
|
||
|
if err != nil {
|
||
|
if err == redis.Nil {
|
||
|
return ErrNilValue
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
err = cbor.Unmarshal(data, &value)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (rc *RedisCache) Delete(key string) {
|
||
|
rc.Client.Del(rc.ctx, key)
|
||
|
}
|