summaryrefslogtreecommitdiff
path: root/pkg/util/lru.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-07-10 13:18:30 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-07-10 13:35:20 -0600
commit27401b6ea459921a6152ab1744da1618358465f4 (patch)
tree2c4f9c096f1a593e65d7f824901e815ca48bfaf0 /pkg/util/lru.go
parent42f6f78e0a32ba0eda707154f8e1ffb4579604ee (diff)
Rename the module, mv pkg lib
Diffstat (limited to 'pkg/util/lru.go')
-rw-r--r--pkg/util/lru.go73
1 files changed, 0 insertions, 73 deletions
diff --git a/pkg/util/lru.go b/pkg/util/lru.go
deleted file mode 100644
index 2b62e69..0000000
--- a/pkg/util/lru.go
+++ /dev/null
@@ -1,73 +0,0 @@
-package util
-
-import (
- "sync"
-
- lru "github.com/hashicorp/golang-lru"
-)
-
-type LRUCache[K comparable, V any] struct {
- initOnce sync.Once
- inner *lru.ARCCache
-}
-
-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 {
- 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 {
- 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 {
- 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)
-}
-
-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
-}