diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:17:02 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-12 16:17:02 -0700 |
commit | cfcc753dc8906817e15b1b7c36b4dc12462d12e4 (patch) | |
tree | f5d2aa0caaa4cb336017ba7595c3425f4aa00bfc /lib/containers/lru.go | |
parent | 29b6b9f997913f13a0bff8bb1278a61302413615 (diff) | |
parent | f76faa4b8debd9c94751a03dd65e46c80a340a82 (diff) |
Merge branch 'lukeshu/fast'
Diffstat (limited to 'lib/containers/lru.go')
-rw-r--r-- | lib/containers/lru.go | 78 |
1 files changed, 0 insertions, 78 deletions
diff --git a/lib/containers/lru.go b/lib/containers/lru.go deleted file mode 100644 index aa372ed..0000000 --- a/lib/containers/lru.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package containers - -import ( - lru "github.com/hashicorp/golang-lru" -) - -// LRUCache is a least-recently-used(ish) cache. A zero LRUCache is -// not usable; it must be initialized with NewLRUCache. -type LRUCache[K comparable, V any] struct { - inner *lru.ARCCache -} - -func NewLRUCache[K comparable, V any](size int) *LRUCache[K, V] { - c := new(LRUCache[K, V]) - c.inner, _ = lru.NewARC(size) - return c -} - -func (c *LRUCache[K, V]) Add(key K, value V) { - c.inner.Add(key, value) -} - -func (c *LRUCache[K, V]) Contains(key K) bool { - return c.inner.Contains(key) -} - -func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { - _value, ok := c.inner.Get(key) - if ok { - //nolint:forcetypeassert // Typed wrapper around untyped lib. - value = _value.(V) - } - return value, ok -} - -func (c *LRUCache[K, V]) Keys() []K { - untyped := c.inner.Keys() - typed := make([]K, len(untyped)) - for i := range untyped { - //nolint:forcetypeassert // Typed wrapper around untyped lib. - typed[i] = untyped[i].(K) - } - return typed -} - -func (c *LRUCache[K, V]) Len() int { - return c.inner.Len() -} - -func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { - _value, ok := c.inner.Peek(key) - if ok { - //nolint:forcetypeassert // Typed wrapper around untyped lib. - value = _value.(V) - } - return value, ok -} - -func (c *LRUCache[K, V]) Purge() { - c.inner.Purge() -} - -func (c *LRUCache[K, V]) Remove(key K) { - c.inner.Remove(key) -} - -func (c *LRUCache[K, V]) GetOrElse(key K, fn func() V) V { - var value V - var ok bool - for value, ok = c.Get(key); !ok; value, ok = c.Get(key) { - c.Add(key, fn()) - } - return value -} |