[cache] Make sure LRUCache actually implements the interface
Update the test to enforce that.
Change-Id: If62864d1d1946ee21f088d017c90eceb4e2cf4cb
diff --git a/cache/cache.go b/cache/cache.go
index 1a3c87a..8d99e1c 100644
--- a/cache/cache.go
+++ b/cache/cache.go
@@ -12,16 +12,16 @@
// Cache is the interface for cache.
type Cache interface {
// Adds a value to the cache.
- Add(key Key, value interface{}) bool
+ Add(key Key, value interface{}) interface{}
// Returns key's value from the cache.
- Get(key Key) (value interface{}, ok bool)
+ Get(key Key) (interface{}, bool)
// Check if a key exsists in cache.
- Contains(key Key) (ok bool)
+ Contains(key Key) (bool)
// Removes a key from the cache.
- Remove(key Key) bool
+ Remove(key Key) interface{}
// Returns the number of items in the cache.
Len() int
@@ -46,15 +46,15 @@
}
// Adds a value to the cache and updates the "recently used"-ness of the key.
-func (c *LRUCache) Add(key Key, value interface{}) {
+func (c *LRUCache) Add(key Key, value interface{}) interface{} {
if c.cache == nil {
c.cache = make(map[interface{}]*list.Element)
c.ll = list.New()
}
if e, ok := c.cache[key]; ok {
c.ll.MoveToFront(e)
- e.Value.(*entry).value = value
- return
+ value, e.Value.(*entry).value = e.Value.(*entry).value, value
+ return value
}
e := c.ll.PushFront(&entry{key, value})
c.cache[key] = e
@@ -62,6 +62,7 @@
v := c.ll.Remove(c.ll.Back())
delete(c.cache, v.(*entry).key)
}
+ return nil
}
// Returns key's value from the cache and updates the "recently used"-ness.
diff --git a/cache/cache_test.go b/cache/cache_test.go
index e03d1f8..389a530 100644
--- a/cache/cache_test.go
+++ b/cache/cache_test.go
@@ -22,7 +22,7 @@
}
func TestAdd(t *testing.T) {
- var cache LRUCache
+ var cache Cache = &LRUCache{}
for _, tc := range testCases {
t.Run(fmt.Sprintf("add %s", tc.key), func(t *testing.T) {
cache.Add(tc.key, tc.val)
@@ -36,7 +36,7 @@
}
func TestRemove(t *testing.T) {
- var cache LRUCache
+ var cache Cache = &LRUCache{}
for _, tc := range testCases {
cache.Add(tc.key, tc.val)
}