2021-05-13 11:49:38 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2021-05-27 19:30:52 +00:00
|
|
|
"bytes"
|
2021-05-13 11:49:38 +00:00
|
|
|
"context"
|
2021-05-27 19:30:52 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
2021-05-14 18:21:13 +00:00
|
|
|
"time"
|
2021-05-13 11:49:38 +00:00
|
|
|
|
|
|
|
"github.com/Secured-Finance/dione/config"
|
|
|
|
"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 {
|
2021-05-27 19:30:52 +00:00
|
|
|
data, err := gobMarshal(value)
|
2021-05-13 11:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:30:52 +00:00
|
|
|
rc.Client.Set(rc.ctx, key, data, 0)
|
2021-05-13 11:49:38 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 18:21:13 +00:00
|
|
|
func (rc *RedisCache) StoreWithTTL(key string, value interface{}, ttl time.Duration) error {
|
2021-05-27 19:30:52 +00:00
|
|
|
data, err := gobMarshal(value)
|
2021-05-14 18:21:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-27 19:30:52 +00:00
|
|
|
rc.Client.Set(rc.ctx, key, data, ttl)
|
2021-05-14 18:21:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:30:52 +00:00
|
|
|
func gobMarshal(val interface{}) ([]byte, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
enc := gob.NewEncoder(buf)
|
|
|
|
err := enc.Encode(val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func gobUnmarshal(data []byte, val interface{}) error {
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
dec := gob.NewDecoder(buf)
|
|
|
|
return dec.Decode(&val)
|
|
|
|
}
|
|
|
|
|
2021-05-13 11:49:38 +00:00
|
|
|
func (rc *RedisCache) Get(key string, value interface{}) error {
|
|
|
|
data, err := rc.Client.Get(rc.ctx, key).Bytes()
|
|
|
|
if err != nil {
|
2021-05-27 19:30:52 +00:00
|
|
|
if errors.Is(err, redis.Nil) {
|
|
|
|
return ErrNotFound
|
2021-05-13 11:49:38 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-27 19:30:52 +00:00
|
|
|
return gobUnmarshal(data, &value)
|
2021-05-13 11:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RedisCache) Delete(key string) {
|
|
|
|
rc.Client.Del(rc.ctx, key)
|
|
|
|
}
|
2021-05-31 19:24:07 +00:00
|
|
|
|
|
|
|
func (rc *RedisCache) Items() map[string]interface{} {
|
|
|
|
return nil // TODO
|
|
|
|
}
|
2021-07-26 21:18:16 +00:00
|
|
|
|
|
|
|
func (rc *RedisCache) Exists(key string) bool {
|
|
|
|
res := rc.Client.Exists(context.TODO(), key)
|
|
|
|
if res.Err() != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if res.Val() == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|