summaryrefslogtreecommitdiff
path: root/lib/containers
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-12-31 11:59:09 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:42:08 -0700
commitb261c2a4f5f028c9d83cef208ccc7d829f841bad (patch)
treee5cd6b5705a8f7d99cc7f9dc7ec9d28035e6ef8f /lib/containers
parentc971b863a0a1f9feb75f31657729db3a03da114c (diff)
tree-wide: Annotate values that I might want to be tuning
Diffstat (limited to 'lib/containers')
-rw-r--r--lib/containers/lru.go26
1 files changed, 3 insertions, 23 deletions
diff --git a/lib/containers/lru.go b/lib/containers/lru.go
index bfda361..aa372ed 100644
--- a/lib/containers/lru.go
+++ b/lib/containers/lru.go
@@ -5,45 +5,30 @@
package containers
import (
- "sync"
-
lru "github.com/hashicorp/golang-lru"
)
// LRUCache is a least-recently-used(ish) cache. A zero LRUCache is
-// usable and has a cache size of 128 items; use NewLRUCache to set a
-// different size.
+// not usable; it must be initialized with NewLRUCache.
type LRUCache[K comparable, V any] struct {
- initOnce sync.Once
- inner *lru.ARCCache
+ inner *lru.ARCCache
}
func NewLRUCache[K comparable, V any](size int) *LRUCache[K, V] {
c := new(LRUCache[K, V])
- c.initOnce.Do(func() {
- c.inner, _ = lru.NewARC(size)
- })
+ c.inner, _ = lru.NewARC(size)
return c
}
-func (c *LRUCache[K, V]) init() {
- c.initOnce.Do(func() {
- c.inner, _ = lru.NewARC(128)
- })
-}
-
func (c *LRUCache[K, V]) Add(key K, value V) {
- c.init()
c.inner.Add(key, value)
}
func (c *LRUCache[K, V]) Contains(key K) bool {
- c.init()
return c.inner.Contains(key)
}
func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) {
- c.init()
_value, ok := c.inner.Get(key)
if ok {
//nolint:forcetypeassert // Typed wrapper around untyped lib.
@@ -53,7 +38,6 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) {
}
func (c *LRUCache[K, V]) Keys() []K {
- c.init()
untyped := c.inner.Keys()
typed := make([]K, len(untyped))
for i := range untyped {
@@ -64,12 +48,10 @@ func (c *LRUCache[K, V]) Keys() []K {
}
func (c *LRUCache[K, V]) Len() int {
- c.init()
return c.inner.Len()
}
func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) {
- c.init()
_value, ok := c.inner.Peek(key)
if ok {
//nolint:forcetypeassert // Typed wrapper around untyped lib.
@@ -79,12 +61,10 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) {
}
func (c *LRUCache[K, V]) Purge() {
- c.init()
c.inner.Purge()
}
func (c *LRUCache[K, V]) Remove(key K) {
- c.init()
c.inner.Remove(key)
}