Skip to content

Commit

Permalink
feat(store): added bigcache
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Oct 13, 2019
1 parent 23439ea commit 4e751dc
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -15,4 +15,4 @@ install:
- go get -t -v ./...

script:
- go test -v ./...
- go test -cover -v ./...
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -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/
51 changes: 51 additions & 0 deletions 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
}
67 changes: 67 additions & 0 deletions 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())
}
47 changes: 47 additions & 0 deletions test/mocks/store/bigcache_client_interface.go

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

0 comments on commit 4e751dc

Please sign in to comment.