summaryrefslogtreecommitdiff
path: root/lib/containers/lru.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-01 22:43:58 -0700
commitd675f41242c043ddc4c6c1a1fb8aabcfd324aae2 (patch)
tree4f2afbce761eb377ad0b0ab2e4fb2f478ff844f5 /lib/containers/lru.go
parent9971e38110d5f90d15c7b78f396f2638b3952a96 (diff)
parent6e1a9fbb1e9a943e04902ed3a4958f6821e39456 (diff)
Merge branch 'lukeshu/lint'
Diffstat (limited to 'lib/containers/lru.go')
-rw-r--r--lib/containers/lru.go38
1 files changed, 14 insertions, 24 deletions
diff --git a/lib/containers/lru.go b/lib/containers/lru.go
index a235a12..aa372ed 100644
--- a/lib/containers/lru.go
+++ b/lib/containers/lru.go
@@ -1,80 +1,70 @@
-// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com>
+// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later
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.
value = _value.(V)
}
return value, ok
}
+
func (c *LRUCache[K, V]) Keys() []K {
- c.init()
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 {
- 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.
value = _value.(V)
}
return value, ok
}
+
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)
}