-
-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ install: | |
- go get -t -v ./... | ||
|
||
script: | ||
- go test -v ./... | ||
- go test -cover -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.