Skip to content

Commit

Permalink
feat(cache): Setting optional extend on hit
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 11, 2023
1 parent ac26ec3 commit 39e9853
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
13 changes: 11 additions & 2 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type App[K comparable, V any] struct {
onMissMany fetchMany[K, V]
ttl time.Duration
concurrency int
extendOnHit bool
}

func New[K comparable, V any](client RedisClient, toKey func(K) string, onMiss fetch[K, V], ttl time.Duration, concurrency int, tracer trace.Tracer) *App[K, V] {
Expand Down Expand Up @@ -78,6 +79,12 @@ func (a *App[K, V]) WithRead(client RedisClient) *App[K, V] {
return a
}

func (a *App[K, V]) WithExtendOnHit() *App[K, V] {
a.extendOnHit = true

return a
}

func getClient(client RedisClient) RedisClient {
if !model.IsNil(client) && client.Enabled() {
return client
Expand Down Expand Up @@ -110,7 +117,9 @@ func (a *App[K, V]) Get(ctx context.Context, id K) (V, error) {
} else if value, ok, err := a.decode([]byte(content)); err != nil {
logUnmarshallError(ctx, key, err)
} else if ok {
a.extendTTL(ctx, key)
if a.extendOnHit {
a.extendTTL(ctx, key)
}

return value, nil
}
Expand Down Expand Up @@ -166,7 +175,7 @@ func (a *App[K, V]) decode(content []byte) (value V, ok bool, err error) {
}

func (a *App[K, V]) extendTTL(ctx context.Context, keys ...string) {
if a.write == nil {
if a.write == nil || len(keys) == 0 {
return
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ func TestGet(t *testing.T) {
case "cached":
mockRedisClient.EXPECT().Load(gomock.Any(), gomock.Any()).Return([]byte(`{"id":8000}`), nil)
mockRedisClient.EXPECT().Expire(gomock.Any(), testCase.args.duration, gomock.Any()).Return(nil)
case "cached no extend":
mockRedisClient.EXPECT().Load(gomock.Any(), gomock.Any()).Return([]byte(`{"id":8000}`), nil)
case "store error":
mockRedisClient.EXPECT().Load(gomock.Any(), gomock.Any()).Return(nil, nil)
mockRedisClient.EXPECT().Store(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("store error"))
Expand All @@ -147,6 +149,9 @@ func TestGet(t *testing.T) {
}

instance := New(mockRedisClient, strconv.Itoa, testCase.args.onMiss, testCase.args.duration, -1, nil)
if intention == "cached" {
instance = instance.WithExtendOnHit()
}

got, gotErr := instance.Get(context.Background(), testCase.args.key)

Expand Down
10 changes: 8 additions & 2 deletions pkg/cache/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func (a App[K, V]) handleListSingle(ctx context.Context, onMissError func(K, err
value, ok, err := a.decode([]byte(values[index]))
if ok {
output[index] = value
extendKeys = append(extendKeys, keys[index])

if a.extendOnHit {
extendKeys = append(extendKeys, keys[index])
}

return nil
}
Expand Down Expand Up @@ -127,7 +130,10 @@ func (a App[K, V]) handleListMany(ctx context.Context, items []K, keys, values [
for index, item := range items {
if value, ok, err := a.decode([]byte(values[index])); ok {
output[index] = value
extendKeys = append(extendKeys, keys[index])

if a.extendOnHit {
extendKeys = append(extendKeys, keys[index])
}

continue
} else if err != nil {
Expand Down

0 comments on commit 39e9853

Please sign in to comment.