summaryrefslogtreecommitdiff
path: root/lib/containers/lru.go
blob: 12446b09d745a9eb13cac0e32c17a84ab8515c0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.
type LRUCache[K comparable, V any] struct {
	initOnce sync.Once
	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)
	})
	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)
}

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
}