Documentation
¶
Index ¶
- Constants
- type Cache
- type CacheParams
- type CacheStats
- type LRU
- func (c *LRU[K, V]) Add(key K, value V) (updated bool)
- func (c *LRU[K, V]) Contains(key K) (ok bool)
- func (c *LRU[K, V]) Get(key K) (value V, ok bool)
- func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool)
- func (c *LRU[K, V]) Keys() []K
- func (c *LRU[K, V]) Len() int
- func (c *LRU[K, V]) Peek(key K) (value V, ok bool)
- func (c *LRU[K, V]) Purge()
- func (c *LRU[K, V]) Remove(key K) (present bool)
- func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool)
- type NoCache
- func (n *NoCache[K, V]) Add(_ K, _ V) (updated, evicted bool)
- func (n *NoCache[K, V]) Contains(_ K) bool
- func (n *NoCache[K, V]) ContainsOrAdd(_ K, val V) (ok, evicted bool)
- func (n *NoCache[K, V]) Get(_ K) (val V, ok bool)
- func (n *NoCache[K, V]) GetQueueLen() (int, int, int)
- func (n *NoCache[K, V]) Keys() []K
- func (n *NoCache[K, V]) Len() int
- func (n *NoCache[K, V]) Params() CacheParams
- func (n *NoCache[K, V]) Peek(_ K) (val V, ok bool)
- func (n *NoCache[K, V]) Purge()
- func (n *NoCache[K, V]) Remove(_ K)
- func (n *NoCache[K, V]) RemoveOldest()
- func (n *NoCache[K, V]) ResetStats()
- func (n *NoCache[K, V]) Stats() CacheStats
- type RefCountedElem
- type TwoQueueCache
- func (c *TwoQueueCache[K, V]) Add(key K, value V) (updated, evicted bool)
- func (c *TwoQueueCache[K, V]) Contains(key K) bool
- func (c *TwoQueueCache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)
- func (c *TwoQueueCache[K, V]) Get(key K) (val V, ok bool)
- func (c *TwoQueueCache[K, V]) GetQueueLen() (int, int, int)
- func (c *TwoQueueCache[K, V]) Keys() []K
- func (c *TwoQueueCache[K, V]) Len() int
- func (c *TwoQueueCache[K, V]) Params() CacheParams
- func (c *TwoQueueCache[K, V]) Peek(key K) (value V, ok bool)
- func (c *TwoQueueCache[K, V]) Purge()
- func (c *TwoQueueCache[K, V]) Remove(key K)
- func (c *TwoQueueCache[K, V]) RemoveOldest()
- func (c *TwoQueueCache[K, V]) ResetStats()
- func (c *TwoQueueCache[K, V]) Size() int
- func (c *TwoQueueCache[K, V]) Stats() CacheStats
Constants ¶
const ( // Default2QRecentRatio is the ratio of the 2Q cache dedicated // to recently added entries that have only been accessed once. Default2QRecentRatio = 0.25 // Default2QGhostEntries is the default ratio of ghost // entries kept to track entries recently evicted Default2QGhostEntries = 0.50 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[KeyType comparable, ValType RefCountedElem] interface { Purge() Add(KeyType, ValType) (updated, evicted bool) Get(KeyType) (ValType, bool) Contains(KeyType) bool Peek(KeyType) (ValType, bool) ContainsOrAdd(KeyType, ValType) (ok, evicted bool) Remove(KeyType) RemoveOldest() Keys() []KeyType Len() int GetQueueLen() (int, int, int) Params() CacheParams Stats() CacheStats ResetStats() }
type CacheParams ¶
type CacheStats ¶
type CacheStats struct { Hits int64 Misses int64 Inserts int64 Evictions int64 Count int64 Size int64 }
func (*CacheStats) Add ¶
func (s *CacheStats) Add(sz int)
func (*CacheStats) Clone ¶
func (s *CacheStats) Clone() (c CacheStats)
func (*CacheStats) Hit ¶
func (s *CacheStats) Hit()
func (*CacheStats) Miss ¶
func (s *CacheStats) Miss()
func (*CacheStats) Rem ¶
func (s *CacheStats) Rem(sz int)
func (*CacheStats) Reset ¶
func (s *CacheStats) Reset()
type LRU ¶
type LRU[K comparable, V RefCountedElem] struct { // contains filtered or unexported fields }
LRU implements a non-thread safe fixed size LRU cache
func NewLRU ¶
func NewLRU[K comparable, V RefCountedElem]() (*LRU[K, V], error)
NewLRU constructs an LRU of the given size
func (*LRU[K, V]) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU[K, V]) Keys ¶
func (c *LRU[K, V]) Keys() []K
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*LRU[K, V]) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*LRU[K, V]) Remove ¶
Remove removes the provided key from the cache, returning if the key was contained.
func (*LRU[K, V]) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
type NoCache ¶
type NoCache[K comparable, V RefCountedElem] struct{}
func NewNoCache ¶
func NewNoCache[K comparable, V RefCountedElem]() *NoCache[K, V]
func (*NoCache[K, V]) ContainsOrAdd ¶
func (*NoCache[K, V]) Params ¶
func (n *NoCache[K, V]) Params() CacheParams
func (*NoCache[K, V]) RemoveOldest ¶
func (n *NoCache[K, V]) RemoveOldest()
func (*NoCache[K, V]) ResetStats ¶
func (n *NoCache[K, V]) ResetStats()
func (*NoCache[K, V]) Stats ¶
func (n *NoCache[K, V]) Stats() CacheStats
type RefCountedElem ¶
type TwoQueueCache ¶
type TwoQueueCache[K comparable, V RefCountedElem] struct { // contains filtered or unexported fields }
TwoQueueCache is a thread-safe fixed size 2Q cache. 2Q is an enhancement over the standard LRU cache in that it tracks both frequently and recently used entries separately. This avoids a burst in access to new entries from evicting frequently used entries. It adds some additional tracking overhead to the standard LRU cache, and is computationally about 2x the cost, and adds some metadata over head. The ARCCache is similar, but does not require setting any parameters.
func New2Q ¶
func New2Q[K comparable, V RefCountedElem](size int) (*TwoQueueCache[K, V], error)
New2Q creates a new TwoQueueCache using the default values for the parameters.
func New2QParams ¶
func New2QParams[K comparable, V RefCountedElem](size int, recentRatio float64, ghostRatio float64) (*TwoQueueCache[K, V], error)
New2QParams creates a new TwoQueueCache using the provided parameter values.
func (*TwoQueueCache[K, V]) Add ¶
func (c *TwoQueueCache[K, V]) Add(key K, value V) (updated, evicted bool)
Add adds a value to the cache.
func (*TwoQueueCache[K, V]) Contains ¶
func (c *TwoQueueCache[K, V]) Contains(key K) bool
Contains is used to check if the cache contains a key without updating recency or frequency.
func (*TwoQueueCache[K, V]) ContainsOrAdd ¶
func (c *TwoQueueCache[K, V]) ContainsOrAdd(key K, value V) (ok, evicted bool)
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 (*TwoQueueCache[K, V]) Get ¶
func (c *TwoQueueCache[K, V]) Get(key K) (val V, ok bool)
Get looks up a key's value from the cache.
func (*TwoQueueCache[K, V]) GetQueueLen ¶
func (c *TwoQueueCache[K, V]) GetQueueLen() (int, int, int)
func (*TwoQueueCache[K, V]) Keys ¶
func (c *TwoQueueCache[K, V]) Keys() []K
Keys returns a slice of the keys in the cache. The frequently used keys are first in the returned slice.
func (*TwoQueueCache[K, V]) Len ¶
func (c *TwoQueueCache[K, V]) Len() int
Len returns the number of items in the cache.
func (*TwoQueueCache[K, V]) Params ¶
func (c *TwoQueueCache[K, V]) Params() CacheParams
func (*TwoQueueCache[K, V]) Peek ¶
func (c *TwoQueueCache[K, V]) Peek(key K) (value V, ok bool)
Peek is used to inspect the cache value of a key without updating recency or frequency or stats.
func (*TwoQueueCache[K, V]) Purge ¶
func (c *TwoQueueCache[K, V]) Purge()
Purge is used to completely clear the cache.
func (*TwoQueueCache[K, V]) Remove ¶
func (c *TwoQueueCache[K, V]) Remove(key K)
Remove removes the provided key from the cache.
func (*TwoQueueCache[K, V]) RemoveOldest ¶
func (c *TwoQueueCache[K, V]) RemoveOldest()
func (*TwoQueueCache[K, V]) ResetStats ¶
func (c *TwoQueueCache[K, V]) ResetStats()
func (*TwoQueueCache[K, V]) Size ¶
func (c *TwoQueueCache[K, V]) Size() int
func (*TwoQueueCache[K, V]) Stats ¶
func (c *TwoQueueCache[K, V]) Stats() CacheStats