From 4e751dc80c2678badcb99041c72bd55c18edd98b Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Mon, 14 Oct 2019 01:38:34 +0200 Subject: [PATCH] feat(store): added bigcache --- .travis.yml | 2 +- Makefile | 1 + store/bigcache.go | 51 ++++++++++++++ store/bigcache_test.go | 67 +++++++++++++++++++ test/mocks/store/bigcache_client_interface.go | 47 +++++++++++++ 5 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 store/bigcache.go create mode 100644 store/bigcache_test.go create mode 100644 test/mocks/store/bigcache_client_interface.go diff --git a/.travis.yml b/.travis.yml index 14907ff..4261faf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,4 +15,4 @@ install: - go get -t -v ./... script: - - go test -v ./... + - go test -cover -v ./... diff --git a/Makefile b/Makefile index 1fc5d8f..48701ff 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ mocks: mockery -case=snake -name=SetterCacheInterface -dir=cache/ -output test/mocks/cache/ mockery -case=snake -name=MetricsInterface -dir=metrics/ -output test/mocks/metrics/ mockery -case=snake -name=StoreInterface -dir=store/ -output test/mocks/store/ + mockery -case=snake -name=BigcacheClientInterface -dir=store/ -output test/mocks/store/ mockery -case=snake -name=MemcacheClientInterface -dir=store/ -output test/mocks/store/ mockery -case=snake -name=RedisClientInterface -dir=store/ -output test/mocks/store/ mockery -case=snake -name=RistrettoClientInterface -dir=store/ -output test/mocks/store/ diff --git a/store/bigcache.go b/store/bigcache.go new file mode 100644 index 0000000..849820b --- /dev/null +++ b/store/bigcache.go @@ -0,0 +1,51 @@ +package store + +import ( + "errors" + "time" +) + +// BigcacheClientInterface represents a allegro/bigcache client +type BigcacheClientInterface interface { + Get(key string) ([]byte, error) + Set(key string, entry []byte) error +} + +const ( + BigcacheType = "bigcache" +) + +// BigcacheStore is a store for Redis +type BigcacheStore struct { + client BigcacheClientInterface +} + +// NewBigcache creates a new store to Bigcache instance(s) +func NewBigcache(client BigcacheClientInterface) *BigcacheStore { + return &BigcacheStore{ + client: client, + } +} + +// Get returns data stored from a given key +func (s *BigcacheStore) Get(key interface{}) (interface{}, error) { + item, err := s.client.Get(key.(string)) + if err != nil { + return nil, err + } + if item == nil { + return nil, errors.New("Unable to retrieve data from bigcache") + } + + return item, err +} + +// Set defines data in Redis for given key idntifier +func (s *BigcacheStore) Set(key interface{}, value interface{}, expiration time.Duration) error { + return s.client.Set(key.(string), value.([]byte)) +} + +// GetType returns the store type +func (s *BigcacheStore) GetType() string { + return BigcacheType +} diff --git a/store/bigcache_test.go b/store/bigcache_test.go new file mode 100644 index 0000000..63abfbe --- /dev/null +++ b/store/bigcache_test.go @@ -0,0 +1,67 @@ +package store + +import ( + "testing" + "time" + + mocksStore "github.com/eko/gache/test/mocks/store" + "github.com/stretchr/testify/assert" +) + +func TestNewBigcache(t *testing.T) { + // Given + client := &mocksStore.BigcacheClientInterface{} + + // When + store := NewBigcache(client) + + // Then + assert.IsType(t, new(BigcacheStore), store) + assert.Equal(t, client, store.client) +} + +func TestBigcacheGet(t *testing.T) { + // Given + cacheKey := "my-key" + cacheValue := []byte("my-cache-value") + + client := &mocksStore.BigcacheClientInterface{} + client.On("Get", cacheKey).Return(cacheValue, nil) + + store := NewBigcache(client) + + // When + value, err := store.Get(cacheKey) + + // Then + assert.Nil(t, err) + assert.Equal(t, cacheValue, value) +} + +func TestBigcacheSet(t *testing.T) { + // Given + cacheKey := "my-key" + cacheValue := []byte("my-cache-value") + expiration := 5 * time.Second + + client := &mocksStore.BigcacheClientInterface{} + client.On("Set", cacheKey, cacheValue).Return(nil) + + store := NewBigcache(client) + + // When + err := store.Set(cacheKey, cacheValue, expiration) + + // Then + assert.Nil(t, err) +} + +func TestBigcacheGetType(t *testing.T) { + // Given + client := &mocksStore.BigcacheClientInterface{} + + store := NewBigcache(client) + + // When - Then + assert.Equal(t, BigcacheType, store.GetType()) +} diff --git a/test/mocks/store/bigcache_client_interface.go b/test/mocks/store/bigcache_client_interface.go new file mode 100644 index 0000000..46eecf8 --- /dev/null +++ b/test/mocks/store/bigcache_client_interface.go @@ -0,0 +1,47 @@ +// Code generated by mockery v1.0.0. DO NOT EDIT. + +package mocks + +import mock "github.com/stretchr/testify/mock" + +// BigcacheClientInterface is an autogenerated mock type for the BigcacheClientInterface type +type BigcacheClientInterface struct { + mock.Mock +} + +// Get provides a mock function with given fields: key +func (_m *BigcacheClientInterface) Get(key string) ([]byte, error) { + ret := _m.Called(key) + + var r0 []byte + if rf, ok := ret.Get(0).(func(string) []byte); ok { + r0 = rf(key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(key) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// Set provides a mock function with given fields: key, entry +func (_m *BigcacheClientInterface) Set(key string, entry []byte) error { + ret := _m.Called(key, entry) + + var r0 error + if rf, ok := ret.Get(0).(func(string, []byte) error); ok { + r0 = rf(key, entry) + } else { + r0 = ret.Error(0) + } + + return r0 +}