Documentation ¶
Index ¶
- type Cache
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) DeletePredicate(predicate func(key K) bool)
- func (c *Cache[K, V]) Exists(key K) bool
- func (c *Cache[K, V]) Hits() int
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Load(key K, options ...Option) (value V, ok bool)
- func (c *Cache[K, V]) LoadOrStore(key K, newValue V, options ...Option) (value V, found bool)
- func (c *Cache[K, V]) MaxAge() time.Duration
- func (c *Cache[K, V]) MaxWeight() int
- func (c *Cache[K, V]) Misses() int
- func (c *Cache[K, V]) SetMaxAge(ttl time.Duration) error
- func (c *Cache[K, V]) SetMaxWeight(weight int) error
- func (c *Cache[K, V]) Store(key K, value V, options ...Option)
- func (c *Cache[K, V]) ToMap() map[K]V
- func (c *Cache[K, V]) Weight() int
- type Option
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 is an LRU cache that enforces a maximum weight capacity and age limit for its elements. The LRU cache performs locking internally and is thread-safe. It also keeps track of the hit/miss statistics.
func NewCache ¶
func NewCache[K comparable, V any]() *Cache[K, V]
NewCache creates a new LRU cache with a weight capacity of 16384 and a maximum age of 1hr.
func (*Cache[K, V]) Clear ¶
func (c *Cache[K, V]) Clear()
Clear empties the cache but does not reset the hit/miss statistics.
func (*Cache[K, V]) Delete ¶
func (c *Cache[K, V]) Delete(key K)
Delete removes an element from the cache by key.
func (*Cache[K, V]) DeletePredicate ¶
Delete removes elements from the cache whose keys match the predicate function.
func (*Cache[K, V]) Hits ¶
Hits returns the total number of cache hits. This number can technically overflow.
func (*Cache[K, V]) LoadOrStore ¶
LoadOrStore looks up an element in the cache. If the element is not found, the new value is stored and returned instead.
func (*Cache[K, V]) MaxAge ¶
MaxAge returns the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.
func (*Cache[K, V]) Misses ¶
Misses returns the total number of cache misses. This number can technically overflow.
func (*Cache[K, V]) SetMaxAge ¶
SetMaxAge sets the age limit of elements in this cache. Elements that are bumped have their life span reset and will therefore survive longer.
func (*Cache[K, V]) SetMaxWeight ¶
SetMaxAge sets the total weight limit of elements in this cache. If not specified, elements default to a weight of 1.
func (*Cache[K, V]) Store ¶
Store inserts an element to the front of the cache. The weight must be 1 or greater and cannot exceed the cache's maximum weight limit.
type Option ¶
type Option func(opts *cacheOptions)
Option is used to customize cache operations.
func Bump ¶
Bump indicates whether or not a loaded element is bumped to the head of the cache. Bumping is the default behavior. This option is applicable to load operations.
func MaxAge ¶
MaxAge indicates to ignore elements that have not been inserted or bumped recently. This option is applicable to load operations.
func NoBump ¶
func NoBump() Option
NoBump indicates not to bump a loaded element to the head of the cache. This option is applicable to load operations.
func Weight ¶
Weight sets the weight of the element stored in the cache. It must be 1 or greater and cannot exceed the cache's maximum weight limit. The default weight is 1. Elements are evicted when the total weight of all elements exceeds the cache's capacity. This option is applicable to store operations.