Documentation ¶
Overview ¶
Package cache implements a LRU cache.
The implementation borrows heavily from SmallLRUCache (originally by Nathan Schrenk). The object maintains a doubly-linked list of elements. When an element is accessed, it is promoted to the head of the list. When space is needed, the element at the tail of the list (the least recently used element) is evicted.
Index ¶
- type Item
- type LRUCache
- func (lru *LRUCache) Clear()
- func (lru *LRUCache) Close()
- func (lru *LRUCache) Delete(key string)
- func (lru *LRUCache) Evictions() int64
- func (lru *LRUCache) ForEach(callback func(value any) bool)
- func (lru *LRUCache) Get(key string) (v any, ok bool)
- func (lru *LRUCache) Hits() int64
- func (lru *LRUCache) Items() []Item
- func (lru *LRUCache) Len() int
- func (lru *LRUCache) MaxCapacity() int64
- func (lru *LRUCache) Misses() int64
- func (lru *LRUCache) Set(key string, value any) bool
- func (lru *LRUCache) SetCapacity(capacity int64)
- func (lru *LRUCache) UsedCapacity() int64
- func (lru *LRUCache) Wait()
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LRUCache ¶
type LRUCache struct {
// contains filtered or unexported fields
}
LRUCache is a typical LRU cache implementation. If the cache reaches the capacity, the least recently used item is deleted from the cache. Note the capacity is not the number of items, but the total sum of the CachedSize() of each item.
func NewLRUCache ¶
NewLRUCache creates a new empty cache with the given capacity.
func (*LRUCache) ForEach ¶ added in v0.10.0
ForEach yields all the values for the cache, ordered from most recently used to least recently used.
func (*LRUCache) Get ¶
Get returns a value from the cache, and marks the entry as most recently used.
func (*LRUCache) Items ¶
Items returns all the values for the cache, ordered from most recently used to least recently used.
func (*LRUCache) MaxCapacity ¶ added in v0.10.0
MaxCapacity returns the cache maximum capacity.
func (*LRUCache) SetCapacity ¶
SetCapacity will set the capacity of the cache. If the capacity is smaller, and the current cache size exceed that capacity, the cache will be shrank.
func (*LRUCache) UsedCapacity ¶ added in v0.10.0
UsedCapacity returns the size of the cache (in bytes)