Skip to content

Commit

Permalink
Merge pull request #19 from fastbill/add-cache-ttl
Browse files Browse the repository at this point in the history
Add cache TTL method
  • Loading branch information
perajovic authored Feb 28, 2020
2 parents 8f0ced4 + dc4a610 commit 27ac336
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Cache interface {
GetJSON(key string, result interface{}) error
Del(key string) error
Close() error
TTL(key string) (time.Duration, error)
}

// RedisClient wraps the REDIS client to provide an implementation of the Cache interface.
Expand Down Expand Up @@ -156,6 +157,21 @@ func (r *RedisClient) Close() error {
return r.Redis.Close()
}

// TTL returns remaining time to live of the given key found in REDIS.
// If the key doesn't exist, it returns ErrNotFound.
func (r *RedisClient) TTL(key string) (time.Duration, error) {
result, err := r.Redis.TTL(key).Result()
if err != nil {
return 0, err
}

if result == -2 {
return 0, ErrNotFound
}

return result, nil
}

// prefixedKey adds the prefix in front of the key separated with ":".
// If no prefix was provided for the client than the key is returned as is.
func (r *RedisClient) prefixedKey(key string) string {
Expand Down
34 changes: 34 additions & 0 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,40 @@ func TestDel(t *testing.T) {
})
}

func TestTTL(t *testing.T) {
t.Run("success", func(t *testing.T) {
withRedis(t, func(redis *miniredis.Miniredis, client *RedisClient) {
err := redis.Set("testPrefix:someKey", "100")
require.NoError(t, err)
redis.SetTTL("testPrefix:someKey", 1*time.Second)

result, err := client.TTL("testPrefix:someKey")

require.NoError(t, err)
assert.Equal(t, 1*time.Second, result)
})
})

t.Run("not found", func(t *testing.T) {
withRedis(t, func(redis *miniredis.Miniredis, client *RedisClient) {
_, err := client.TTL("testPrefix:someKey")

require.Error(t, err)
assert.Equal(t, ErrNotFound, err)
})
})

t.Run("failure", func(t *testing.T) {
withRedis(t, func(redis *miniredis.Miniredis, client *RedisClient) {
redis.Close()
_, err := client.TTL("testPrefix:someKey")

require.Error(t, err)
assert.NotEqual(t, ErrNotFound, err)
})
})
}

func withRedis(t *testing.T, fn func(redis *miniredis.Miniredis, client *RedisClient)) {
redis, err := miniredis.Run()
assert.NoError(t, err, "error in test setup")
Expand Down
11 changes: 11 additions & 0 deletions cache/cachemock/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ func (m *Cache) SetInt(key string, value int64, expiration time.Duration) error
return args.Error(0)
}

// TTL is a mock implementation of cache.Cache#TTL.
func (m *Cache) TTL(key string) (time.Duration, error) {
args := m.Called(key)

if args.Get(0) != nil {
return args.Get(0).(time.Duration), args.Error(1)
}

return 0, args.Error(1)
}

// SetJSON is a mock implementation of cache.Cache#SetJSON.
func (m *Cache) SetJSON(key string, value interface{}, expiration time.Duration) error {
args := m.Called(key, value, expiration)
Expand Down

0 comments on commit 27ac336

Please sign in to comment.