k3s/pkg/util/lru.go
Brad Davidson 62d2737faa lint: unchecked-type-assertion
Adds a generic wrapper around lru.Cache

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
2025-12-18 11:20:07 -08:00

54 lines
990 B
Go

package util
import "k8s.io/utils/lru"
// Cache is a generic wrapper around lru.Cache that handles type assertions when
// retrieving cached entries.
type Cache[T any] struct {
cache *lru.Cache
}
func NewCache[T any](size int) *Cache[T] {
return &Cache[T]{
cache: lru.New(size),
}
}
func NewCacheWithEvictionFunc[T any](size int, f lru.EvictionFunc) *Cache[T] {
return &Cache[T]{
cache: lru.NewWithEvictionFunc(size, f),
}
}
func (c *Cache[T]) Add(key lru.Key, value T) {
c.cache.Add(key, value)
}
func (c *Cache[T]) Clear() {
c.cache.Clear()
}
func (c *Cache[T]) Get(key lru.Key) (value T, ok bool) {
v, ok := c.cache.Get(key)
if !ok {
return value, ok
}
value, ok = v.(T)
return value, ok
}
func (c *Cache[T]) Len() int {
return c.cache.Len()
}
func (c *Cache[T]) Remove(key lru.Key) {
c.cache.Remove(key)
}
func (c *Cache[T]) RemoveOldest() {
c.cache.RemoveOldest()
}
func (c *Cache[T]) SetEvictionFunc(f lru.EvictionFunc) {
c.cache.SetEvictionFunc(f)
}