mirror of https://github.com/ethereum/go-ethereum
common/lru: add generic LRU implementation (#26162)
It seems there is no fully typed library implementation of an LRU cache. So I wrote one. Method names are the same as github.com/hashicorp/golang-lru, and the new type can be used as a drop-in replacement. Two reasons to do this: - It's much easier to understand what a cache is for when the types are right there. - Performance: the new implementation is slightly faster and performs zero memory allocations in Add when the cache is at capacity. Overall, memory usage of the cache is much reduced because keys are values are no longer wrapped in interface.pull/26180/head
parent
f58ebd9696
commit
9afc6816d2
@ -0,0 +1,223 @@ |
|||||||
|
// Copyright 2022 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
// Package lru implements generically-typed LRU caches.
|
||||||
|
package lru |
||||||
|
|
||||||
|
// BasicLRU is a simple LRU cache.
|
||||||
|
//
|
||||||
|
// This type is not safe for concurrent use.
|
||||||
|
// The zero value is not valid, instances must be created using NewCache.
|
||||||
|
type BasicLRU[K comparable, V any] struct { |
||||||
|
list *list[K] |
||||||
|
items map[K]cacheItem[K, V] |
||||||
|
cap int |
||||||
|
} |
||||||
|
|
||||||
|
type cacheItem[K any, V any] struct { |
||||||
|
elem *listElem[K] |
||||||
|
value V |
||||||
|
} |
||||||
|
|
||||||
|
// NewBasicLRU creates a new LRU cache.
|
||||||
|
func NewBasicLRU[K comparable, V any](capacity int) BasicLRU[K, V] { |
||||||
|
if capacity < 0 { |
||||||
|
capacity = 1 |
||||||
|
} |
||||||
|
c := BasicLRU[K, V]{ |
||||||
|
items: make(map[K]cacheItem[K, V]), |
||||||
|
list: newList[K](), |
||||||
|
cap: capacity, |
||||||
|
} |
||||||
|
return c |
||||||
|
} |
||||||
|
|
||||||
|
// Add adds a value to the cache. Returns true if an item was evicted to store the new item.
|
||||||
|
func (c *BasicLRU[K, V]) Add(key K, value V) (evicted bool) { |
||||||
|
item, ok := c.items[key] |
||||||
|
if ok { |
||||||
|
// Already exists in cache.
|
||||||
|
item.value = value |
||||||
|
c.items[key] = item |
||||||
|
c.list.moveToFront(item.elem) |
||||||
|
return false |
||||||
|
} |
||||||
|
|
||||||
|
var elem *listElem[K] |
||||||
|
if c.Len() >= c.cap { |
||||||
|
elem = c.list.removeLast() |
||||||
|
delete(c.items, elem.v) |
||||||
|
evicted = true |
||||||
|
} else { |
||||||
|
elem = new(listElem[K]) |
||||||
|
} |
||||||
|
|
||||||
|
// Store the new item.
|
||||||
|
// Note that, if another item was evicted, we re-use its list element here.
|
||||||
|
elem.v = key |
||||||
|
c.items[key] = cacheItem[K, V]{elem, value} |
||||||
|
c.list.pushElem(elem) |
||||||
|
return evicted |
||||||
|
} |
||||||
|
|
||||||
|
// Contains reports whether the given key exists in the cache.
|
||||||
|
func (c *BasicLRU[K, V]) Contains(key K) bool { |
||||||
|
_, ok := c.items[key] |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
// Get retrieves a value from the cache. This marks the key as recently used.
|
||||||
|
func (c *BasicLRU[K, V]) Get(key K) (value V, ok bool) { |
||||||
|
item, ok := c.items[key] |
||||||
|
if !ok { |
||||||
|
return value, false |
||||||
|
} |
||||||
|
c.list.moveToFront(item.elem) |
||||||
|
return item.value, true |
||||||
|
} |
||||||
|
|
||||||
|
// GetOldest retrieves the least-recently-used item.
|
||||||
|
// Note that this does not update the item's recency.
|
||||||
|
func (c *BasicLRU[K, V]) GetOldest() (key K, value V, ok bool) { |
||||||
|
lastElem := c.list.last() |
||||||
|
if lastElem == nil { |
||||||
|
return key, value, false |
||||||
|
} |
||||||
|
key = lastElem.v |
||||||
|
item := c.items[key] |
||||||
|
return key, item.value, true |
||||||
|
} |
||||||
|
|
||||||
|
// Len returns the current number of items in the cache.
|
||||||
|
func (c *BasicLRU[K, V]) Len() int { |
||||||
|
return len(c.items) |
||||||
|
} |
||||||
|
|
||||||
|
// Peek retrieves a value from the cache, but does not mark the key as recently used.
|
||||||
|
func (c *BasicLRU[K, V]) Peek(key K) (value V, ok bool) { |
||||||
|
item, ok := c.items[key] |
||||||
|
return item.value, ok |
||||||
|
} |
||||||
|
|
||||||
|
// Purge empties the cache.
|
||||||
|
func (c *BasicLRU[K, V]) Purge() { |
||||||
|
c.list.init() |
||||||
|
for k := range c.items { |
||||||
|
delete(c.items, k) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Remove drops an item from the cache. Returns true if the key was present in cache.
|
||||||
|
func (c *BasicLRU[K, V]) Remove(key K) bool { |
||||||
|
item, ok := c.items[key] |
||||||
|
if ok { |
||||||
|
delete(c.items, key) |
||||||
|
c.list.remove(item.elem) |
||||||
|
} |
||||||
|
return ok |
||||||
|
} |
||||||
|
|
||||||
|
// RemoveOldest drops the least recently used item.
|
||||||
|
func (c *BasicLRU[K, V]) RemoveOldest() (key K, value V, ok bool) { |
||||||
|
lastElem := c.list.last() |
||||||
|
if lastElem == nil { |
||||||
|
return key, value, false |
||||||
|
} |
||||||
|
|
||||||
|
key = lastElem.v |
||||||
|
item := c.items[key] |
||||||
|
delete(c.items, key) |
||||||
|
c.list.remove(lastElem) |
||||||
|
return key, item.value, true |
||||||
|
} |
||||||
|
|
||||||
|
// Keys returns all keys in the cache.
|
||||||
|
func (c *BasicLRU[K, V]) Keys() []K { |
||||||
|
keys := make([]K, 0, len(c.items)) |
||||||
|
return c.list.appendTo(keys) |
||||||
|
} |
||||||
|
|
||||||
|
// list is a doubly-linked list holding items of type he.
|
||||||
|
// The zero value is not valid, use newList to create lists.
|
||||||
|
type list[T any] struct { |
||||||
|
root listElem[T] |
||||||
|
} |
||||||
|
|
||||||
|
type listElem[T any] struct { |
||||||
|
next *listElem[T] |
||||||
|
prev *listElem[T] |
||||||
|
v T |
||||||
|
} |
||||||
|
|
||||||
|
func newList[T any]() *list[T] { |
||||||
|
l := new(list[T]) |
||||||
|
l.init() |
||||||
|
return l |
||||||
|
} |
||||||
|
|
||||||
|
// init reinitializes the list, making it empty.
|
||||||
|
func (l *list[T]) init() { |
||||||
|
l.root.next = &l.root |
||||||
|
l.root.prev = &l.root |
||||||
|
} |
||||||
|
|
||||||
|
// push adds an element to the front of the list.
|
||||||
|
func (l *list[T]) pushElem(e *listElem[T]) { |
||||||
|
e.prev = &l.root |
||||||
|
e.next = l.root.next |
||||||
|
l.root.next = e |
||||||
|
e.next.prev = e |
||||||
|
} |
||||||
|
|
||||||
|
// moveToFront makes 'node' the head of the list.
|
||||||
|
func (l *list[T]) moveToFront(e *listElem[T]) { |
||||||
|
e.prev.next = e.next |
||||||
|
e.next.prev = e.prev |
||||||
|
l.pushElem(e) |
||||||
|
} |
||||||
|
|
||||||
|
// remove removes an element from the list.
|
||||||
|
func (l *list[T]) remove(e *listElem[T]) { |
||||||
|
e.prev.next = e.next |
||||||
|
e.next.prev = e.prev |
||||||
|
e.next, e.prev = nil, nil |
||||||
|
} |
||||||
|
|
||||||
|
// removeLast removes the last element of the list.
|
||||||
|
func (l *list[T]) removeLast() *listElem[T] { |
||||||
|
last := l.last() |
||||||
|
if last != nil { |
||||||
|
l.remove(last) |
||||||
|
} |
||||||
|
return last |
||||||
|
} |
||||||
|
|
||||||
|
// last returns the last element of the list, or nil if the list is empty.
|
||||||
|
func (l *list[T]) last() *listElem[T] { |
||||||
|
e := l.root.prev |
||||||
|
if e == &l.root { |
||||||
|
return nil |
||||||
|
} |
||||||
|
return e |
||||||
|
} |
||||||
|
|
||||||
|
// appendTo appends all list elements to a slice.
|
||||||
|
func (l *list[T]) appendTo(slice []T) []T { |
||||||
|
for e := l.root.prev; e != &l.root; e = e.prev { |
||||||
|
slice = append(slice, e.v) |
||||||
|
} |
||||||
|
return slice |
||||||
|
} |
@ -0,0 +1,240 @@ |
|||||||
|
// Copyright 2022 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package lru |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"io" |
||||||
|
"math/rand" |
||||||
|
"testing" |
||||||
|
) |
||||||
|
|
||||||
|
// Some of these test cases were adapted
|
||||||
|
// from https://github.com/hashicorp/golang-lru/blob/master/simplelru/lru_test.go
|
||||||
|
|
||||||
|
func TestBasicLRU(t *testing.T) { |
||||||
|
cache := NewBasicLRU[int, int](128) |
||||||
|
|
||||||
|
for i := 0; i < 256; i++ { |
||||||
|
cache.Add(i, i) |
||||||
|
} |
||||||
|
if cache.Len() != 128 { |
||||||
|
t.Fatalf("bad len: %v", cache.Len()) |
||||||
|
} |
||||||
|
|
||||||
|
// Check that Keys returns least-recent key first.
|
||||||
|
keys := cache.Keys() |
||||||
|
if len(keys) != 128 { |
||||||
|
t.Fatal("wrong Keys() length", len(keys)) |
||||||
|
} |
||||||
|
for i, k := range keys { |
||||||
|
v, ok := cache.Peek(k) |
||||||
|
if !ok { |
||||||
|
t.Fatalf("expected key %d be present", i) |
||||||
|
} |
||||||
|
if v != k { |
||||||
|
t.Fatalf("expected %d == %d", k, v) |
||||||
|
} |
||||||
|
if v != i+128 { |
||||||
|
t.Fatalf("wrong value at key %d: %d, want %d", i, v, i+128) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for i := 0; i < 128; i++ { |
||||||
|
_, ok := cache.Get(i) |
||||||
|
if ok { |
||||||
|
t.Fatalf("%d should be evicted", i) |
||||||
|
} |
||||||
|
} |
||||||
|
for i := 128; i < 256; i++ { |
||||||
|
_, ok := cache.Get(i) |
||||||
|
if !ok { |
||||||
|
t.Fatalf("%d should not be evicted", i) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
for i := 128; i < 192; i++ { |
||||||
|
ok := cache.Remove(i) |
||||||
|
if !ok { |
||||||
|
t.Fatalf("%d should be in cache", i) |
||||||
|
} |
||||||
|
ok = cache.Remove(i) |
||||||
|
if ok { |
||||||
|
t.Fatalf("%d should not be in cache", i) |
||||||
|
} |
||||||
|
_, ok = cache.Get(i) |
||||||
|
if ok { |
||||||
|
t.Fatalf("%d should be deleted", i) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Request item 192.
|
||||||
|
cache.Get(192) |
||||||
|
// It should be the last item returned by Keys().
|
||||||
|
for i, k := range cache.Keys() { |
||||||
|
if (i < 63 && k != i+193) || (i == 63 && k != 192) { |
||||||
|
t.Fatalf("out of order key: %v", k) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
cache.Purge() |
||||||
|
if cache.Len() != 0 { |
||||||
|
t.Fatalf("bad len: %v", cache.Len()) |
||||||
|
} |
||||||
|
if _, ok := cache.Get(200); ok { |
||||||
|
t.Fatalf("should contain nothing") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestBasicLRUAddExistingKey(t *testing.T) { |
||||||
|
cache := NewBasicLRU[int, int](1) |
||||||
|
|
||||||
|
cache.Add(1, 1) |
||||||
|
cache.Add(1, 2) |
||||||
|
|
||||||
|
v, _ := cache.Get(1) |
||||||
|
if v != 2 { |
||||||
|
t.Fatal("wrong value:", v) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// This test checks GetOldest and RemoveOldest.
|
||||||
|
func TestBasicLRUGetOldest(t *testing.T) { |
||||||
|
cache := NewBasicLRU[int, int](128) |
||||||
|
for i := 0; i < 256; i++ { |
||||||
|
cache.Add(i, i) |
||||||
|
} |
||||||
|
|
||||||
|
k, _, ok := cache.GetOldest() |
||||||
|
if !ok { |
||||||
|
t.Fatalf("missing") |
||||||
|
} |
||||||
|
if k != 128 { |
||||||
|
t.Fatalf("bad: %v", k) |
||||||
|
} |
||||||
|
|
||||||
|
k, _, ok = cache.RemoveOldest() |
||||||
|
if !ok { |
||||||
|
t.Fatalf("missing") |
||||||
|
} |
||||||
|
if k != 128 { |
||||||
|
t.Fatalf("bad: %v", k) |
||||||
|
} |
||||||
|
|
||||||
|
k, _, ok = cache.RemoveOldest() |
||||||
|
if !ok { |
||||||
|
t.Fatalf("missing oldest item") |
||||||
|
} |
||||||
|
if k != 129 { |
||||||
|
t.Fatalf("wrong oldest item: %v", k) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Test that Add returns true/false if an eviction occurred
|
||||||
|
func TestBasicLRUAddReturnValue(t *testing.T) { |
||||||
|
cache := NewBasicLRU[int, int](1) |
||||||
|
if cache.Add(1, 1) { |
||||||
|
t.Errorf("first add shouldn't have evicted") |
||||||
|
} |
||||||
|
if !cache.Add(2, 2) { |
||||||
|
t.Errorf("second add should have evicted") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// This test verifies that Contains doesn't change item recency.
|
||||||
|
func TestBasicLRUContains(t *testing.T) { |
||||||
|
cache := NewBasicLRU[int, int](2) |
||||||
|
cache.Add(1, 1) |
||||||
|
cache.Add(2, 2) |
||||||
|
if !cache.Contains(1) { |
||||||
|
t.Errorf("1 should be in the cache") |
||||||
|
} |
||||||
|
cache.Add(3, 3) |
||||||
|
if cache.Contains(1) { |
||||||
|
t.Errorf("Contains should not have updated recency of 1") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func BenchmarkLRU(b *testing.B) { |
||||||
|
var ( |
||||||
|
capacity = 1000 |
||||||
|
indexes = make([]int, capacity*20) |
||||||
|
keys = make([]string, capacity) |
||||||
|
values = make([][]byte, capacity) |
||||||
|
) |
||||||
|
for i := range indexes { |
||||||
|
indexes[i] = rand.Intn(capacity) |
||||||
|
} |
||||||
|
for i := range keys { |
||||||
|
b := make([]byte, 32) |
||||||
|
rand.Read(b) |
||||||
|
keys[i] = string(b) |
||||||
|
rand.Read(b) |
||||||
|
values[i] = b |
||||||
|
} |
||||||
|
|
||||||
|
var sink []byte |
||||||
|
|
||||||
|
b.Run("Add/BasicLRU", func(b *testing.B) { |
||||||
|
cache := NewBasicLRU[int, int](capacity) |
||||||
|
for i := 0; i < b.N; i++ { |
||||||
|
cache.Add(i, i) |
||||||
|
} |
||||||
|
}) |
||||||
|
b.Run("Get/BasicLRU", func(b *testing.B) { |
||||||
|
cache := NewBasicLRU[string, []byte](capacity) |
||||||
|
for i := 0; i < capacity; i++ { |
||||||
|
index := indexes[i] |
||||||
|
cache.Add(keys[index], values[index]) |
||||||
|
} |
||||||
|
|
||||||
|
b.ResetTimer() |
||||||
|
for i := 0; i < b.N; i++ { |
||||||
|
k := keys[indexes[i%len(indexes)]] |
||||||
|
v, ok := cache.Get(k) |
||||||
|
if ok { |
||||||
|
sink = v |
||||||
|
} |
||||||
|
} |
||||||
|
}) |
||||||
|
|
||||||
|
// // vs. github.com/hashicorp/golang-lru/simplelru
|
||||||
|
// b.Run("Add/simplelru.LRU", func(b *testing.B) {
|
||||||
|
// cache, _ := simplelru.NewLRU(capacity, nil)
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// cache.Add(i, i)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
// b.Run("Get/simplelru.LRU", func(b *testing.B) {
|
||||||
|
// cache, _ := simplelru.NewLRU(capacity, nil)
|
||||||
|
// for i := 0; i < capacity; i++ {
|
||||||
|
// index := indexes[i]
|
||||||
|
// cache.Add(keys[index], values[index])
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// b.ResetTimer()
|
||||||
|
// for i := 0; i < b.N; i++ {
|
||||||
|
// k := keys[indexes[i%len(indexes)]]
|
||||||
|
// v, ok := cache.Get(k)
|
||||||
|
// if ok {
|
||||||
|
// sink = v.([]byte)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
|
fmt.Fprintln(io.Discard, sink) |
||||||
|
} |
@ -0,0 +1,95 @@ |
|||||||
|
// Copyright 2022 The go-ethereum Authors
|
||||||
|
// This file is part of the go-ethereum library.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Lesser General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package lru |
||||||
|
|
||||||
|
import "sync" |
||||||
|
|
||||||
|
// Cache is a LRU cache.
|
||||||
|
// This type is safe for concurrent use.
|
||||||
|
type Cache[K comparable, V any] struct { |
||||||
|
cache BasicLRU[K, V] |
||||||
|
mu sync.Mutex |
||||||
|
} |
||||||
|
|
||||||
|
// NewCache creates an LRU cache.
|
||||||
|
func NewCache[K comparable, V any](capacity int) *Cache[K, V] { |
||||||
|
return &Cache[K, V]{cache: NewBasicLRU[K, V](capacity)} |
||||||
|
} |
||||||
|
|
||||||
|
// Add adds a value to the cache. Returns true if an item was evicted to store the new item.
|
||||||
|
func (c *Cache[K, V]) Add(key K, value V) (evicted bool) { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Add(key, value) |
||||||
|
} |
||||||
|
|
||||||
|
// Contains reports whether the given key exists in the cache.
|
||||||
|
func (c *Cache[K, V]) Contains(key K) bool { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Contains(key) |
||||||
|
} |
||||||
|
|
||||||
|
// Get retrieves a value from the cache. This marks the key as recently used.
|
||||||
|
func (c *Cache[K, V]) Get(key K) (value V, ok bool) { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Get(key) |
||||||
|
} |
||||||
|
|
||||||
|
// Len returns the current number of items in the cache.
|
||||||
|
func (c *Cache[K, V]) Len() int { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Len() |
||||||
|
} |
||||||
|
|
||||||
|
// Peek retrieves a value from the cache, but does not mark the key as recently used.
|
||||||
|
func (c *Cache[K, V]) Peek(key K) (value V, ok bool) { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Peek(key) |
||||||
|
} |
||||||
|
|
||||||
|
// Purge empties the cache.
|
||||||
|
func (c *Cache[K, V]) Purge() { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
c.cache.Purge() |
||||||
|
} |
||||||
|
|
||||||
|
// Remove drops an item from the cache. Returns true if the key was present in cache.
|
||||||
|
func (c *Cache[K, V]) Remove(key K) bool { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Remove(key) |
||||||
|
} |
||||||
|
|
||||||
|
// Keys returns all keys of items currently in the LRU.
|
||||||
|
func (c *Cache[K, V]) Keys() []K { |
||||||
|
c.mu.Lock() |
||||||
|
defer c.mu.Unlock() |
||||||
|
|
||||||
|
return c.cache.Keys() |
||||||
|
} |
Loading…
Reference in new issue