diff --git a/cache/cache.go b/cache/cache.go index 0783f34..bf23a26 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,11 +1,15 @@ package cache -import "errors" +import ( + "errors" + "time" +) var ErrNilValue = errors.New("value is empty") type Cache interface { Store(key string, value interface{}) error + StoreWithTTL(key string, value interface{}, ttl time.Duration) error Get(key string, value interface{}) error Delete(key string) } diff --git a/cache/inmemory_cache.go b/cache/inmemory_cache.go index 80b84fd..c036275 100644 --- a/cache/inmemory_cache.go +++ b/cache/inmemory_cache.go @@ -1,6 +1,8 @@ package cache import ( + "time" + "github.com/VictoriaMetrics/fastcache" "github.com/fxamacker/cbor/v2" ) @@ -31,6 +33,10 @@ func (imc *InMemoryCache) Store(key string, value interface{}) error { return nil } +func (imc *InMemoryCache) StoreWithTTL(key string, value interface{}, ttl time.Duration) error { + return imc.Store(key, value) // fastcache doesn't support ttl for values +} + func (imc *InMemoryCache) Get(key string, v interface{}) error { data := make([]byte, 0) imc.cache.GetBig(data, []byte(key)) diff --git a/cache/redis_cache.go b/cache/redis_cache.go index 70b9f98..0788dbe 100644 --- a/cache/redis_cache.go +++ b/cache/redis_cache.go @@ -2,6 +2,7 @@ package cache import ( "context" + "time" "github.com/Secured-Finance/dione/config" "github.com/fxamacker/cbor/v2" @@ -37,6 +38,17 @@ func (rc *RedisCache) Store(key string, value interface{}) error { return nil } +func (rc *RedisCache) StoreWithTTL(key string, value interface{}, ttl time.Duration) error { + mRes, err := cbor.Marshal(value) + if err != nil { + return err + } + + rc.Client.Set(rc.ctx, key, mRes, ttl) + + return nil +} + func (rc *RedisCache) Get(key string, value interface{}) error { data, err := rc.Client.Get(rc.ctx, key).Bytes() if err != nil {