Documentation
¶
Overview ¶
Package lru contains a typed Least-Recently-Used cache.
Index ¶
- type Cache
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Contains(key K) bool
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) DeleteOldest() (key K, val V, ok bool)
- func (c *Cache[K, V]) DumpHTML(w io.Writer)
- func (c *Cache[K, V]) ForEach(fn func(K, V))
- func (c *Cache[K, V]) Get(key K) V
- func (c *Cache[K, V]) GetOk(key K) (value V, ok bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) PeekOk(key K) (value V, ok bool)
- func (c *Cache[K, V]) Set(key K, value V)
- func (c *Cache[K, V]) Size() int64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // MaxSize is the maximum number of cache "size" units before // an item is evicted. Zero means no limit. // // By default, the size of an entry is 1, unless EntrySize is set. MaxSize int64 // EntrySize returns the size of a single cache entry. This is used to // calculate the total size of the cache and enforce the MaxSize limit. // If nil, the size of an entry is 1. EntrySize func(key K, value V) int64 // contains filtered or unexported fields }
Cache is container type keyed by K, storing V, optionally evicting the least recently used items if a maximum size is exceeded.
The zero value is valid to use.
It is not safe for concurrent access.
The current implementation is just the traditional LRU linked list; a future implementation may be more advanced to avoid pathological cases.
func (*Cache[K, V]) Contains ¶
Contains reports whether c contains key.
If found, key is moved to the front of the LRU.
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete removes the provided key from the cache if it was present.
func (*Cache[K, V]) DeleteOldest ¶
DeleteOldest removes the item from the cache that was least recently accessed.
It returns the deleted item, if any.
func (*Cache[K, V]) DumpHTML ¶
DumpHTML writes the state of the cache to the given writer, formatted as an HTML table.
func (*Cache[K, V]) ForEach ¶
func (c *Cache[K, V]) ForEach(fn func(K, V))
ForEach calls fn for each entry in the cache, from most recently used to least recently used.
func (*Cache[K, V]) Get ¶
func (c *Cache[K, V]) Get(key K) V
Get looks up a key's value from the cache, returning either the value or the zero value if it not present.
If found, key is moved to the front of the LRU.
func (*Cache[K, V]) GetOk ¶
GetOk looks up a key's value from the cache, also reporting whether it was present.
If found, key is moved to the front of the LRU.
func (*Cache[K, V]) PeekOk ¶
PeekOk looks up the key's value from the cache, also reporting whether it was present.
Unlike GetOk, PeekOk does not move key to the front of the LRU. This should mostly be used for non-intrusive debug inspection of the cache.