Documentation
¶
Index ¶
- Constants
- type Cache
- func (c *Cache[K, V]) Add(key K, value V) (evicted bool)
- func (c *Cache[K, V]) Cap() int
- func (c *Cache[K, V]) Contains(key K) bool
- func (c *Cache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)
- func (c *Cache[K, V]) Get(key K) (value V, ok bool)
- func (c *Cache[K, V]) GetOldest() (key K, value V, ok bool)
- func (c *Cache[K, V]) Keys() []K
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Peek(key K) (value V, ok bool)
- func (c *Cache[K, V]) PeekOrAdd(key K, value V) (previous V, ok, evicted bool)
- func (c *Cache[K, V]) Purge()
- func (c *Cache[K, V]) Remove(key K) (present bool)
- func (c *Cache[K, V]) RemoveOldest() (key K, value V, ok bool)
- func (c *Cache[K, V]) Resize(size int) (evicted int)
- func (c *Cache[K, V]) Values() []V
Constants ¶
const (
// DefaultEvictedBufferSize defines the default buffer size to store evicted key/val
DefaultEvictedBufferSize = 16
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache is a thread-safe fixed size LRU cache.
func New ¶
func New[K comparable, V any](size int) (*Cache[K, V], error)
New creates an LRU of the given size.
func NewWithEvict ¶
func NewWithEvict[K comparable, V any](size int, onEvicted func(key K, value V)) (c *Cache[K, V], err error)
NewWithEvict constructs a fixed size cache with the given eviction callback.
func (*Cache[K, V]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*Cache[K, V]) ContainsOrAdd ¶
ContainsOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.
func (*Cache[K, V]) Keys ¶
func (c *Cache[K, V]) Keys() []K
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*Cache[K, V]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*Cache[K, V]) PeekOrAdd ¶
PeekOrAdd checks if a key is in the cache without updating the recent-ness or deleting it for being stale, and if not, adds the value. Returns whether found and whether an eviction occurred.
func (*Cache[K, V]) Purge ¶
func (c *Cache[K, V]) Purge()
Purge is used to completely clear the cache.
func (*Cache[K, V]) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.