Skip to content

Commit

Permalink
Implement GetWithTTL for freecache
Browse files Browse the repository at this point in the history
  • Loading branch information
corinapurcarea committed Aug 17, 2020
1 parent 2dd429c commit a7ab8a7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ mocks:
mockgen -source=store/memcache.go -destination=test/mocks/store/clients/memcache_interface.go -package=mocks
mockgen -source=store/redis.go -destination=test/mocks/store/clients/redis_interface.go -package=mocks
mockgen -source=store/ristretto.go -destination=test/mocks/store/clients/ristretto_interface.go -package=mocks
mockgen -source=store/freecache.go -destination=test/mocks/store/clients/freecache_interface.go -package=mocks
4 changes: 2 additions & 2 deletions cache/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ChainCache struct {
setChannel chan *chainKeyValue
}

// NewChain instanciates a new cache aggregator
// NewChain instantiates a new cache aggregator
func NewChain(caches ...SetterCacheInterface) *ChainCache {
chain := &ChainCache{
caches: caches,
Expand Down Expand Up @@ -112,7 +112,7 @@ func (c *ChainCache) Clear() error {
return nil
}

// GetCaches returns all Chaind caches
// GetCaches returns all Chained caches
func (c *ChainCache) GetCaches() []SetterCacheInterface {
return c.caches
}
Expand Down
19 changes: 19 additions & 0 deletions store/freecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
type FreecacheClientInterface interface {
Get(key []byte) (value []byte, err error)
GetInt(key int64) (value []byte, err error)
TTL(key []byte) (timeLeft uint32, err error)
Set(key, value []byte, expireSeconds int) (err error)
SetInt(key int64, value []byte, expireSeconds int) (err error)
Del(key []byte) (affected bool)
Expand Down Expand Up @@ -56,7 +57,25 @@ func (f *FreecacheStore) Get(key interface{}) (interface{}, error) {
}

return nil, errors.New("key type not supported by Freecache store")
}

// GetWithTTL returns data stored from a given key and its corresponding TTL
func (f *FreecacheStore) GetWithTTL(key interface{}) (interface{}, time.Duration, error) {
if k, ok := key.(string); ok {
result, err := f.client.Get([]byte(k))
if err != nil {
return nil, 0, errors.New("value not found in Freecache store")
}

ttl, err := f.client.TTL([]byte(k))
if err != nil {
return nil, 0, errors.New("value not found in Freecache store")
}

return result, time.Duration(ttl) * time.Second, err
}

return nil, 0, errors.New("key type not supported by Freecache store")
}

// Set sets a key, value and expiration for a cache entry and stores it in the cache.
Expand Down
71 changes: 71 additions & 0 deletions store/freecache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,78 @@ func TestFreecacheGetInvalidKey(t *testing.T) {

_, err := s.Get([]byte("key1"))
assert.Error(t, err, "key type not supported by Freecache store")
}

func TestFreecacheGetWithTTL(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cacheKey := "my-key"
cacheValue := []byte("my-cache-value")

client := mocksStore.NewMockFreecacheClientInterface(ctrl)
client.EXPECT().Get([]byte(cacheKey)).Return(cacheValue, nil)
client.EXPECT().TTL([]byte(cacheKey)).Return(uint32(5), nil)

options := &Options{Expiration: 3 * time.Second}
store := NewFreecache(client, options)

// When
value, ttl, err := store.GetWithTTL(cacheKey)

// Then
assert.Nil(t, err)
assert.Equal(t, cacheValue, value)
assert.Equal(t, 5*time.Second, ttl)
}

func TestFreecacheGetWithTTLWhenMissingItem(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cacheKey := "my-key"
expectedErr := errors.New("value not found in Freecache store")

client := mocksStore.NewMockFreecacheClientInterface(ctrl)
client.EXPECT().Get([]byte(cacheKey)).Return(nil, expectedErr)

options := &Options{Expiration: 3 * time.Second}
store := NewFreecache(client, options)

// When
value, ttl, err := store.GetWithTTL(cacheKey)

// Then
assert.Equal(t, expectedErr, err)
assert.Nil(t, value)
assert.Equal(t, 0*time.Second, ttl)
}

func TestFreecacheGetWithTTLWhenErrorAtTTL(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cacheKey := "my-key"
cacheValue := []byte("my-cache-value")
expectedErr := errors.New("value not found in Freecache store")

client := mocksStore.NewMockFreecacheClientInterface(ctrl)
client.EXPECT().Get([]byte(cacheKey)).Return(cacheValue, nil)
client.EXPECT().TTL([]byte(cacheKey)).Return(uint32(0), expectedErr)

options := &Options{Expiration: 3 * time.Second}
store := NewFreecache(client, options)

// When
value, ttl, err := store.GetWithTTL(cacheKey)

// Then
assert.Equal(t, expectedErr, err)
assert.Nil(t, value)
assert.Equal(t, 0*time.Second, ttl)
}

func TestFreecacheSet(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions test/mocks/store/clients/freecache_interface.go

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

0 comments on commit a7ab8a7

Please sign in to comment.