Skip to content

Commit

Permalink
make cache more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Wenquan Xing committed Dec 22, 2017
1 parent 62c7c29 commit ae26fc6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
10 changes: 5 additions & 5 deletions common/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ import (
type Cache interface {
// Get retrieves an element based on a key, returning nil if the element
// does not exist
Get(key string) interface{}
Get(key interface{}) interface{}

// Put adds an element to the cache, returning the previous element
Put(key string, value interface{}) interface{}
Put(key interface{}, value interface{}) interface{}

// PutIfNotExist puts a value associated with a given key if it does not exist
PutIfNotExist(key string, value interface{}) (interface{}, error)
PutIfNotExist(key interface{}, value interface{}) (interface{}, error)

// Delete deletes an element in the cache
Delete(key string)
Delete(key interface{})

// Release decrements the ref count of a pinned element. If the ref count
// drops to 0, the element can be evicted from the cache.
Release(key string)
Release(key interface{})

// Size returns the number of entries currently stored in the Cache
Size() int
Expand Down
18 changes: 9 additions & 9 deletions common/cache/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
type lru struct {
mut sync.Mutex
byAccess *list.List
byKey map[string]*list.Element
byKey map[interface{}]*list.Element
maxSize int
ttl time.Duration
pin bool
Expand All @@ -51,7 +51,7 @@ func New(maxSize int, opts *Options) Cache {

return &lru{
byAccess: list.New(),
byKey: make(map[string]*list.Element, opts.InitialCapacity),
byKey: make(map[interface{}]*list.Element, opts.InitialCapacity),
ttl: opts.TTL,
maxSize: maxSize,
pin: opts.Pin,
Expand All @@ -74,7 +74,7 @@ func NewLRUWithInitialCapacity(initialCapacity, maxSize int) Cache {
}

// Get retrieves the value stored under the given key
func (c *lru) Get(key string) interface{} {
func (c *lru) Get(key interface{}) interface{} {
c.mut.Lock()
defer c.mut.Unlock()

Expand Down Expand Up @@ -104,7 +104,7 @@ func (c *lru) Get(key string) interface{} {
}

// Put puts a new value associated with a given key, returning the existing value (if present)
func (c *lru) Put(key string, value interface{}) interface{} {
func (c *lru) Put(key interface{}, value interface{}) interface{} {
if c.pin {
panic("Cannot use Put API in Pin mode. Use Delete and PutIfNotExist if necessary")
}
Expand All @@ -113,7 +113,7 @@ func (c *lru) Put(key string, value interface{}) interface{} {
}

// PutIfNotExist puts a value associated with a given key if it does not exist
func (c *lru) PutIfNotExist(key string, value interface{}) (interface{}, error) {
func (c *lru) PutIfNotExist(key interface{}, value interface{}) (interface{}, error) {
existing, err := c.putInternal(key, value, false)
if err != nil {
return nil, err
Expand All @@ -128,7 +128,7 @@ func (c *lru) PutIfNotExist(key string, value interface{}) (interface{}, error)
}

// Delete deletes a key, value pair associated with a key
func (c *lru) Delete(key string) {
func (c *lru) Delete(key interface{}) {
c.mut.Lock()
defer c.mut.Unlock()

Expand All @@ -143,7 +143,7 @@ func (c *lru) Delete(key string) {
}

// Release decrements the ref count of a pinned element.
func (c *lru) Release(key string) {
func (c *lru) Release(key interface{}) {
c.mut.Lock()
defer c.mut.Unlock()

Expand All @@ -162,7 +162,7 @@ func (c *lru) Size() int {

// Put puts a new value associated with a given key, returning the existing value (if present)
// allowUpdate flag is used to control overwrite behavior if the value exists
func (c *lru) putInternal(key string, value interface{}, allowUpdate bool) (interface{}, error) {
func (c *lru) putInternal(key interface{}, value interface{}, allowUpdate bool) (interface{}, error) {
c.mut.Lock()
defer c.mut.Unlock()

Expand Down Expand Up @@ -219,7 +219,7 @@ func (c *lru) putInternal(key string, value interface{}, allowUpdate bool) (inte
}

type cacheEntry struct {
key string
key interface{}
expiration time.Time
value interface{}
refCount int
Expand Down
26 changes: 26 additions & 0 deletions common/cache/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"github.com/stretchr/testify/assert"
)

type keyType struct {
dummyString string
dummyInt int
}

func TestLRU(t *testing.T) {
cache := NewLRU(5)

Expand Down Expand Up @@ -62,6 +67,27 @@ func TestLRU(t *testing.T) {
assert.Nil(t, cache.Get("A"))
}

func TestGenerics(t *testing.T) {
key := keyType{
dummyString: "some random key",
dummyInt: 59,
}
value := "some random value"

cache := NewLRU(5)
cache.Put(key, value)

assert.Equal(t, value, cache.Get(key))
assert.Equal(t, value, cache.Get(keyType{
dummyString: "some random key",
dummyInt: 59,
}))
assert.Nil(t, cache.Get(keyType{
dummyString: "some other random key",
dummyInt: 56,
}))
}

func TestLRUWithTTL(t *testing.T) {
cache := New(5, &Options{
TTL: time.Millisecond * 100,
Expand Down

0 comments on commit ae26fc6

Please sign in to comment.