Skip to content

Commit

Permalink
fixup! cache/lru: update LRU cache to use new generic linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Nov 9, 2022
1 parent f37d47f commit a95b214
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
34 changes: 7 additions & 27 deletions cache/lru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,7 @@ import (
)

// elementMap is an alias for a map from a generic interface to a list.Element.
type elementMap[K comparable, V any] struct {
m map[K]*Element[V]
}

// Del deletes an item from the cache.
func (e *elementMap[K, V]) Del(key K) {
delete(e.m, key)
}

// Lookup attempts to lookup a value in the cache.
func (e *elementMap[K, V]) Lookup(key K) (*Element[V], bool) {
el, ok := e.m[key]
return el, ok
}

// Store attempts to store an item in the cache.
func (e *elementMap[K, V]) Store(key K, val *Element[V]) {
e.m[key] = val
}
type elementMap[K comparable, V any] map[K]V

// entry represents a (key,value) pair entry in the Cache. The Cache's list
// stores entries which let us get the cache key when an entry is evicted.
Expand All @@ -51,7 +33,7 @@ type Cache[K comparable, V cache.Value] struct {

// cache is a generic cache which allows us to find an elements position
// in the ll list from a given key.
cache elementMap[K, entry[K, V]]
cache elementMap[K, *Element[entry[K, V]]]

// mtx is used to make sure the Cache is thread-safe.
mtx sync.RWMutex
Expand All @@ -63,9 +45,7 @@ func NewCache[K comparable, V cache.Value](capacity uint64) *Cache[K, V] {
return &Cache[K, V]{
capacity: capacity,
ll: NewList[entry[K, V]](),
cache: elementMap[K, entry[K, V]]{
m: make(map[K]*Element[entry[K, V]]),
},
cache: make(map[K]*Element[entry[K, V]]),
}
}

Expand Down Expand Up @@ -104,7 +84,7 @@ func (c *Cache[K, V]) evict(needed uint64) (bool, error) {

// Remove the element from the cache.
c.ll.Remove(elr)
c.cache.Del(ce.key)
delete(c.cache, ce.key)
evicted = true
}
}
Expand Down Expand Up @@ -132,7 +112,7 @@ func (c *Cache[K, V]) Put(key K, value V) (bool, error) {
defer c.mtx.Unlock()

// If the element already exists, remove it and decrease cache's size.
el, ok := c.cache.Lookup(key)
el, ok := c.cache[key]
if ok {
es, err := el.Value.value.Size()
if err != nil {
Expand All @@ -152,7 +132,7 @@ func (c *Cache[K, V]) Put(key K, value V) (bool, error) {

// We have made enough space in the cache, so just insert it.
el = c.ll.PushFront(entry[K, V]{key, value})
c.cache.Store(key, el)
c.cache[key] = el
c.size += vs

return evicted, nil
Expand All @@ -166,7 +146,7 @@ func (c *Cache[K, V]) Get(key K) (V, error) {

var defaultVal V

el, ok := c.cache.Lookup(key)
el, ok := c.cache[key]
if !ok {
// Element not found in the cache.
return defaultVal, cache.ErrElementNotFound
Expand Down
4 changes: 2 additions & 2 deletions cache/lru/lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestElementSizeCapacityEvictsEverything(t *testing.T) {
// Insert element with size=capacity of cache, should evict everything.
c.Put(4, &sizeable{value: 4, size: 3})
require.Equal(t, c.Len(), 1)
require.Equal(t, len(c.cache.m), 1)
require.Equal(t, len(c.cache), 1)
four := getSizeableValue(c.Get(4))
require.Equal(t, four, 4)

Expand All @@ -110,7 +110,7 @@ func TestElementSizeCapacityEvictsEverything(t *testing.T) {
// Insert element with size=capacity of cache.
c.Put(4, &sizeable{value: 4, size: 6})
require.Equal(t, c.Len(), 1)
require.Equal(t, len(c.cache.m), 1)
require.Equal(t, len(c.cache), 1)
four = getSizeableValue(c.Get(4))
require.Equal(t, four, 4)
}
Expand Down

0 comments on commit a95b214

Please sign in to comment.