From a95b214093db80f347cbee726be4fca734591c59 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Tue, 8 Nov 2022 22:00:49 -0800 Subject: [PATCH] fixup! cache/lru: update LRU cache to use new generic linked list --- cache/lru/lru.go | 34 +++++++--------------------------- cache/lru/lru_test.go | 4 ++-- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/cache/lru/lru.go b/cache/lru/lru.go index c08c8255..a58e98f4 100644 --- a/cache/lru/lru.go +++ b/cache/lru/lru.go @@ -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. @@ -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 @@ -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]]), } } @@ -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 } } @@ -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 { @@ -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 @@ -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 diff --git a/cache/lru/lru_test.go b/cache/lru/lru_test.go index fbbebad5..b594c26a 100644 --- a/cache/lru/lru_test.go +++ b/cache/lru/lru_test.go @@ -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) @@ -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) }