Skip to content

Commit

Permalink
Merge pull request #266 from blampe/blampe/ttl
Browse files Browse the repository at this point in the history
ristretto: return correct ttl
  • Loading branch information
eko authored Jan 7, 2025
2 parents a125c59 + 41bad53 commit 33d992d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
7 changes: 6 additions & 1 deletion store/ristretto/ristretto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
// RistrettoClientInterface represents a dgraph-io/ristretto client
type RistrettoClientInterface interface {
Get(key any) (any, bool)
GetTTL(key any) (time.Duration, bool)
SetWithTTL(key, value any, cost int64, ttl time.Duration) bool
Del(key any)
Clear()
Expand Down Expand Up @@ -55,7 +56,11 @@ func (s *RistrettoStore) Get(_ context.Context, key any) (any, error) {
// GetWithTTL returns data stored from a given key and its corresponding TTL
func (s *RistrettoStore) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error) {
value, err := s.Get(ctx, key)
return value, 0, err
if err != nil {
return value, 0, err
}
ttl, _ := s.client.GetTTL(key)
return value, ttl, nil
}

// Set defines data in Ristretto memory cache for given key identifier
Expand Down
27 changes: 24 additions & 3 deletions store/ristretto/ristretto_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion store/ristretto/ristretto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func TestRistrettoGetWithTTL(t *testing.T) {

client := NewMockRistrettoClientInterface(ctrl)
client.EXPECT().Get(cacheKey).Return(cacheValue, true)
client.EXPECT().GetTTL(cacheKey).Return(time.Minute, true)

store := NewRistretto(client)

Expand All @@ -89,7 +90,7 @@ func TestRistrettoGetWithTTL(t *testing.T) {
// Then
assert.Nil(t, err)
assert.Equal(t, cacheValue, value)
assert.Equal(t, 0*time.Second, ttl)
assert.Equal(t, time.Minute, ttl)
}

func TestRistrettoGetWithTTLWhenError(t *testing.T) {
Expand Down

0 comments on commit 33d992d

Please sign in to comment.