-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from eko/cache-memcached
feat(store): added memcache support
- Loading branch information
Showing
7 changed files
with
233 additions
and
13 deletions.
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
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
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,59 @@ | ||
package store | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/bradfitz/gomemcache/memcache" | ||
) | ||
|
||
// MemcacheClientInterface represents a bradfitz/gomemcache client | ||
type MemcacheClientInterface interface { | ||
Get(key string) (item *memcache.Item, err error) | ||
Set(item *memcache.Item) error | ||
} | ||
|
||
const ( | ||
MemcacheType = "memcache" | ||
) | ||
|
||
// MemcacheStore is a store for Redis | ||
type MemcacheStore struct { | ||
client MemcacheClientInterface | ||
} | ||
|
||
// NewMemcache creates a new store to Memcache instance(s) | ||
func NewMemcache(client MemcacheClientInterface) *MemcacheStore { | ||
return &MemcacheStore{ | ||
client: client, | ||
} | ||
} | ||
|
||
// Get returns data stored from a given key | ||
func (s *MemcacheStore) 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 memcache") | ||
} | ||
|
||
return item.Value, err | ||
} | ||
|
||
// Set defines data in Redis for given key idntifier | ||
func (s *MemcacheStore) Set(key interface{}, value interface{}, expiration time.Duration) error { | ||
item := &memcache.Item{ | ||
Key: key.(string), | ||
Value: value.([]byte), | ||
Expiration: int32(expiration.Seconds()), | ||
} | ||
|
||
return s.client.Set(item) | ||
} | ||
|
||
// GetType returns the store type | ||
func (s *MemcacheStore) GetType() string { | ||
return MemcacheType | ||
} |
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,74 @@ | ||
package store | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/bradfitz/gomemcache/memcache" | ||
mocksStore "github.com/eko/gache/test/mocks/store" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewMemcache(t *testing.T) { | ||
// Given | ||
client := &mocksStore.MemcacheClientInterface{} | ||
|
||
// When | ||
store := NewMemcache(client) | ||
|
||
// Then | ||
assert.IsType(t, new(MemcacheStore), store) | ||
assert.Equal(t, client, store.client) | ||
} | ||
|
||
func TestMemcacheGet(t *testing.T) { | ||
// Given | ||
cacheKey := "my-key" | ||
cacheValue := []byte("my-cache-value") | ||
|
||
client := &mocksStore.MemcacheClientInterface{} | ||
client.On("Get", cacheKey).Return(&memcache.Item{ | ||
Value: cacheValue, | ||
}, nil) | ||
|
||
store := NewMemcache(client) | ||
|
||
// When | ||
value, err := store.Get(cacheKey) | ||
|
||
// Then | ||
assert.Nil(t, err) | ||
assert.Equal(t, cacheValue, value) | ||
} | ||
|
||
func TestMemcacheSet(t *testing.T) { | ||
// Given | ||
cacheKey := "my-key" | ||
cacheValue := []byte("my-cache-value") | ||
expiration := 5 * time.Second | ||
|
||
client := &mocksStore.MemcacheClientInterface{} | ||
client.On("Set", &memcache.Item{ | ||
Key: cacheKey, | ||
Value: cacheValue, | ||
Expiration: int32(expiration.Seconds()), | ||
}).Return(nil) | ||
|
||
store := NewMemcache(client) | ||
|
||
// When | ||
err := store.Set(cacheKey, cacheValue, expiration) | ||
|
||
// Then | ||
assert.Nil(t, err) | ||
} | ||
|
||
func TestMemcacheGetType(t *testing.T) { | ||
// Given | ||
client := &mocksStore.MemcacheClientInterface{} | ||
|
||
store := NewMemcache(client) | ||
|
||
// When - Then | ||
assert.Equal(t, MemcacheType, store.GetType()) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.