Documentation ¶
Index ¶
- type HashKeyCallback
- type LRU
- func (lru *LRU[K, V]) Add(key K, value V) (evicted bool)
- func (lru *LRU[K, V]) AddWithLifetime(key K, value V, lifetime time.Duration) (evicted bool)
- func (lru *LRU[K, V]) Contains(key K) (ok bool)
- func (lru *LRU[K, V]) Get(key K) (value V, ok bool)
- func (lru *LRU[K, V]) Keys() []K
- func (lru *LRU[K, V]) Len() int
- func (lru *LRU[K, V]) Peek(key K) (value V, ok bool)
- func (lru *LRU[K, V]) PrintStats()
- func (lru *LRU[K, V]) Purge()
- func (lru *LRU[K, V]) Remove(key K) (removed bool)
- func (lru *LRU[K, V]) SetLifetime(lifetime time.Duration)
- func (lru *LRU[K, V]) SetOnEvict(onEvict OnEvictCallback[K, V])
- type OnEvictCallback
- type SyncedLRU
- func (lru *SyncedLRU[K, V]) Add(key K, value V) (evicted bool)
- func (lru *SyncedLRU[K, V]) AddWithLifetime(key K, value V, lifetime time.Duration) (evicted bool)
- func (lru *SyncedLRU[K, V]) Contains(key K) (ok bool)
- func (lru *SyncedLRU[K, V]) Get(key K) (value V, ok bool)
- func (lru *SyncedLRU[K, V]) Keys() []K
- func (lru *SyncedLRU[K, V]) Len() int
- func (lru *SyncedLRU[K, V]) Peek(key K) (value V, ok bool)
- func (lru *SyncedLRU[K, V]) PrintStats()
- func (lru *SyncedLRU[K, V]) Purge()
- func (lru *SyncedLRU[K, V]) Remove(key K) (removed bool)
- func (lru *SyncedLRU[K, V]) SetLifetime(lifetime time.Duration)
- func (lru *SyncedLRU[K, V]) SetOnEvict(onEvict OnEvictCallback[K, V])
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashKeyCallback ¶
type HashKeyCallback[K comparable] func(K) uint32
HashKeyCallback is the function that creates a hash from the passed key.
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU implements a non-thread safe fixed size LRU cache.
func New ¶
func New[K comparable, V any](cap uint32, hash HashKeyCallback[K]) (*LRU[K, V], error)
New constructs an LRU with the given capacity of elements. The hash function calculates a hash value from the keys.
func NewWithSize ¶ added in v0.3.0
func NewWithSize[K comparable, V any](cap, size uint32, hash HashKeyCallback[K]) ( *LRU[K, V], error)
NewWithSize constructs an LRU with the given capacity and size. The hash function calculates a hash value from the keys. A size greater than the capacity increases memory consumption and decreases the CPU consumption by reducing the chance of collisions. Size must not be lower than the capacity.
func (*LRU[K, V]) Add ¶
Add adds a key:value to the cache. Returns true, true if key was updated and eviction occurred.
func (*LRU[K, V]) AddWithLifetime ¶ added in v0.7.0
AddWithLifetime adds a key:value to the cache with a lifetime. Returns true, true if key was updated and eviction occurred.
func (*LRU[K, V]) Contains ¶
Contains checks for the existence of a key, without changing its recent-ness.
func (*LRU[K, V]) Get ¶
Get looks up a key's value from the cache, setting it as the most recently used item.
func (*LRU[K, V]) Keys ¶
func (lru *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 looks up a key's value from the cache, without changing its recent-ness.
func (*LRU[K, V]) PrintStats ¶
func (lru *LRU[K, V]) PrintStats()
func (*LRU[K, V]) Purge ¶ added in v0.4.0
func (lru *LRU[K, V]) Purge()
Purge purges all data (key and value) from the LRU.
func (*LRU[K, V]) Remove ¶
Remove removes the key from the cache. The return value indicates whether the key existed or not.
func (*LRU[K, V]) SetLifetime ¶ added in v0.3.0
SetLifetime sets the default lifetime of LRU elements. Lifetime 0 means "forever".
func (*LRU[K, V]) SetOnEvict ¶ added in v0.3.0
func (lru *LRU[K, V]) SetOnEvict(onEvict OnEvictCallback[K, V])
SetOnEvict sets the OnEvict callback function. The onEvict function is called for each evicted lru entry.
type OnEvictCallback ¶
type OnEvictCallback[K comparable, V any] func(K, V)
OnEvictCallback is the function type for Config.OnEvict.
type SyncedLRU ¶ added in v0.8.0
type SyncedLRU[K comparable, V any] struct { // contains filtered or unexported fields }
func NewSynced ¶ added in v0.8.0
func NewSynced[K comparable, V any](cap uint32, hash HashKeyCallback[K]) (*SyncedLRU[K, V], error)
NewSynced creates a new thread-safe LRU hashmap with the given capacity.
func NewSyncedWithSize ¶ added in v0.8.0
func NewSyncedWithSize[K comparable, V any](cap, size uint32, hash HashKeyCallback[K]) (*SyncedLRU[K, V], error)
func (*SyncedLRU[K, V]) Add ¶ added in v0.8.0
Add adds a key:value to the cache. Returns true, true if key was updated and eviction occurred.
func (*SyncedLRU[K, V]) AddWithLifetime ¶ added in v0.8.0
AddWithLifetime adds a key:value to the cache with a lifetime. Returns true, true if key was updated and eviction occurred.
func (*SyncedLRU[K, V]) Contains ¶ added in v0.8.0
Contains checks for the existence of a key, without changing its recent-ness.
func (*SyncedLRU[K, V]) Get ¶ added in v0.8.0
Get looks up a key's value from the cache, setting it as the most recently used item.
func (*SyncedLRU[K, V]) Keys ¶ added in v0.8.0
func (lru *SyncedLRU[K, V]) Keys() []K
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*SyncedLRU[K, V]) Len ¶ added in v0.8.0
Len returns the number of elements stored in the cache.
func (*SyncedLRU[K, V]) Peek ¶ added in v0.8.0
Peek looks up a key's value from the cache, without changing its recent-ness.
func (*SyncedLRU[K, V]) PrintStats ¶ added in v0.8.0
func (lru *SyncedLRU[K, V]) PrintStats()
func (*SyncedLRU[K, V]) Purge ¶ added in v0.8.0
func (lru *SyncedLRU[K, V]) Purge()
Purge purges all data (key and value) from the LRU.
func (*SyncedLRU[K, V]) Remove ¶ added in v0.8.0
Remove removes the key from the cache. The return value indicates whether the key existed or not.
func (*SyncedLRU[K, V]) SetLifetime ¶ added in v0.8.0
SetLifetime sets the default lifetime of LRU elements. Lifetime 0 means "forever".
func (*SyncedLRU[K, V]) SetOnEvict ¶ added in v0.8.0
func (lru *SyncedLRU[K, V]) SetOnEvict(onEvict OnEvictCallback[K, V])
SetOnEvict sets the OnEvict callback function. The onEvict function is called for each evicted lru entry.