Documentation
¶
Overview ¶
Package cache contains data structures that are useful to build caches.
The types provided by the package are generic building blocks implementing caching algorithms. Synchronization in caching strategies is often very specific to the application and harder to generalize, so the types provided by this package do not make opinionated choices on how synchronization should be handled, which makes them unsafe to use concurrently from multiple goroutines
Index ¶
- type Cache
- func (c *Cache[K, V]) Delete(key K) (value V, deleted bool)
- func (c *Cache[K, V]) Evict() (key K, value V, evicted bool)
- func (c *Cache[K, V]) Init(backend Interface[K, V])
- func (c *Cache[K, V]) Insert(key K, value V) (previous V, replaced bool)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Lookup(key K) (value V, found bool)
- func (c *Cache[K, V]) Range(f func(K, V) bool)
- func (c *Cache[K, V]) Stats() Stats
- type Interface
- type LRU
- func (lru *LRU[K, V]) Delete(key K) (value V, deleted bool)
- func (lru *LRU[K, V]) Evict() (key K, value V, evicted bool)
- func (lru *LRU[K, V]) Insert(key K, value V) (previous V, replaced bool)
- func (lru *LRU[K, V]) Len() int
- func (lru *LRU[K, V]) Lookup(key K) (value V, found bool)
- func (lru *LRU[K, V]) Range(f func(K, V) bool)
- type Stats
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 { // contains filtered or unexported fields }
Cache wraps an underlying caching implementation, adding measures of usage.
By default, a LRU caching strategy is used.
type Interface ¶
type Interface[K comparable, V any] interface { // Returns the number of items in the cache. Len() int // Inserts an item in the cache, returning the previous value associated // with the cache key. Insert(key K, value V) (previous V, replaced bool) // Returns the value associated with the given key in the cache. Lookup(key K) (value V, found bool) // Deletes an item from the cache. Delete(key K) (value V, deleted bool) // Evicts an item from the cache. Evict() (key K, value V, evicted bool) // Calls f for each entry in the cache. The order in which entries are // presented is unspecified. If f returns false, iteration stops. Range(f func(K, V) bool) }
Interface is the interface implemented by caches.
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU is an Interface implementation which caches elements and tracks least recently used items as candidates for eviction.