The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
woop/internal/utils/lrucache/lrucache.go

33 lines
651 B

package lrucache
import lru "github.com/hashicorp/golang-lru"
type Cache[K comparable, V any] struct {
cache *lru.Cache
}
func NewCache[K comparable, V any](size int) *Cache[K, V] {
c, _ := lru.New(size)
return &Cache[K, V]{
cache: c,
}
}
func (c *Cache[K, V]) Get(key K) (V, bool) {
v, ok := c.cache.Get(key)
if !ok {
var out V
return out, false
}
return v.(V), true
}
func (c *Cache[K, V]) Set(key K, value V) {
c.cache.Add(key, value)
}
// Contains checks if a key is in the cache, without updating the
// recent-ness or deleting it for being stale.
func (c *Cache[K, V]) Contains(key K) bool {
return c.cache.Contains(key)
}