Skip to content

Commit

Permalink
LocalCache: Support ttl per key (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf authored Feb 1, 2024
1 parent bab2620 commit a785a81
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 29 deletions.
12 changes: 3 additions & 9 deletions pkg/zcache/combined_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
prefix := os.Getenv("PREFIX")
suite.cacheRemoteBrokenBestEffort, err = NewCombinedCache(
&CombinedConfig{
Local: &LocalConfig{
EvictionInSeconds: 10,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: "0.0.0.0",
},
Expand All @@ -48,9 +46,7 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {
suite.Nil(err)

suite.cacheOkNotBestEffort, err = NewCombinedCache(&CombinedConfig{
Local: &LocalConfig{
EvictionInSeconds: 10,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: mr.Addr(),
},
Expand All @@ -62,9 +58,7 @@ func (suite *CombinedCacheTestSuite) SetupSuite() {

suite.cacheRemoteBrokenNotBestEffort, err = NewCombinedCache(
&CombinedConfig{
Local: &LocalConfig{
EvictionInSeconds: 10,
},
Local: &LocalConfig{},
Remote: &RemoteConfig{
Addr: "0.0.0.0",
},
Expand Down
14 changes: 4 additions & 10 deletions pkg/zcache/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/allegro/bigcache/v3"
"github.com/go-redis/redis/v8"
"go.uber.org/zap"
"math"
"time"
)

Expand All @@ -27,9 +26,8 @@ type RemoteConfig struct {
}

type LocalConfig struct {
EvictionInSeconds int
Prefix string
Logger *zap.Logger
Prefix string
Logger *zap.Logger
}

func (c *RemoteConfig) ToRedisConfig() *redis.Options {
Expand All @@ -51,12 +49,8 @@ func (c *RemoteConfig) ToRedisConfig() *redis.Options {
}

func (c *LocalConfig) ToBigCacheConfig() bigcache.Config {
eviction := time.Duration(c.EvictionInSeconds) * time.Second
if c.EvictionInSeconds < 0 {
eviction = time.Duration(math.MaxInt64)
}

return bigcache.DefaultConfig(eviction)
evictionTime := time.Duration(100*365*24) * time.Hour // 100 years
return bigcache.DefaultConfig(evictionTime)
}

type CombinedConfig struct {
Expand Down
47 changes: 42 additions & 5 deletions pkg/zcache/local_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ import (
"time"
)

type CacheItem struct {
Value []byte `json:"value"`
ExpiresAt int64 `json:"expires_at"`
}

func NewCacheItem(value []byte, ttl time.Duration) CacheItem {
return CacheItem{
Value: value,
ExpiresAt: time.Now().Add(ttl).Unix(),
}
}

func (item CacheItem) IsExpired() bool {
return time.Now().Unix() > item.ExpiresAt
}

type LocalCache interface {
ZCache
}
Expand All @@ -23,17 +39,24 @@ type localCache struct {
appName string
}

func (c *localCache) Set(_ context.Context, key string, value interface{}, _ time.Duration) error {
func (c *localCache) Set(_ context.Context, key string, value interface{}, ttl time.Duration) error {
realKey := getKeyWithPrefix(c.prefix, key)

c.logger.Sugar().Debugf("set key on local cache, fullKey: [%s], value: [%v]", realKey, value)
val, err := json.Marshal(value)
b, err := json.Marshal(value)
if err != nil {
c.logger.Sugar().Errorf("error marshalling cache item value, key: [%s], err: [%s]", realKey, err)
return err
}

err = c.client.Set(realKey, val)
cacheItem := NewCacheItem(b, ttl)
itemBytes, err := json.Marshal(cacheItem)
if err != nil {
c.logger.Sugar().Errorf("error marshalling cache item, key: [%s], err: [%s]", realKey, err)
return err
}

c.logger.Sugar().Debugf("set key on local cache with TTL, key: [%s], value: [%v], ttl: [%v]", realKey, value, ttl)
if err = c.client.Set(realKey, itemBytes); err != nil {
c.logger.Sugar().Errorf("error setting new key on local cache, fullKey: [%s], err: [%s]", realKey, err)
}

Expand All @@ -55,7 +78,21 @@ func (c *localCache) Get(_ context.Context, key string, data interface{}) error

return err
}
return json.Unmarshal(val, &data)

var cachedItem CacheItem
if err := json.Unmarshal(val, &cachedItem); err != nil {
c.logger.Sugar().Errorf("error unmarshalling cache item, key: [%s], err: [%s]", realKey, err)
return err
}

if cachedItem.IsExpired() {
c.logger.Sugar().Debugf("key expired on local cache, key: [%s]", realKey)
_ = c.client.Delete(realKey)
return errors.New("cache item expired")
}

c.logger.Sugar().Debugf("key retrieved from local cache, key: [%s]", realKey)
return json.Unmarshal(cachedItem.Value, data)
}

func (c *localCache) Delete(_ context.Context, key string) error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/zcache/local_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ func (suite *LocalCacheTestSuite) SetupSuite() {
prefix := os.Getenv("PREFIX")
var err error
config := LocalConfig{
EvictionInSeconds: 100000,
Prefix: prefix,
Prefix: prefix,
}
suite.cache, err = NewLocalCache(&config)
suite.Nil(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/zcache/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {

```go
func main() {
config := zcache.LocalConfig{Eviction: 12}
config := zcache.LocalConfig{}
cache, err := zcache.NewLocalCache(config)
if err != nil {
// Handle error
Expand All @@ -82,7 +82,7 @@ func main() {

```go
func main() {
localConfig := zcache.LocalConfig{Eviction: 12}
localConfig := zcache.LocalConfig{}
remoteConfig := zcache.RemoteConfig{Addr: "localhost:6379"}
config := zcache.CombinedConfig{Local: localConfig, Remote: remoteConfig, isRemoteBestEffort: false}
cache, err := zcache.NewCombinedCache(config)
Expand Down
1 change: 0 additions & 1 deletion pkg/zcache/zcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func NewCombinedCache(combinedConfig *CombinedConfig) (CombinedCache, error) {
}

// Set global configs on local cache config
localCacheConfig.EvictionInSeconds = combinedConfig.GlobalTtlSeconds
localCacheConfig.Prefix = combinedConfig.GlobalPrefix
localCacheConfig.Logger = combinedConfig.GlobalLogger

Expand Down

0 comments on commit a785a81

Please sign in to comment.