Documentation
¶
Overview ¶
Package evcache implements a concurrent key-value cache with capacity overflow eviction, item expiry and deduplication.
Index ¶
- Constants
- type Cache
- func (c *Cache[K, V]) Evict(key K) (value V, evicted bool)
- func (c *Cache[K, V]) Fetch(key K, f func() (V, error)) (value V, err error)
- func (c *Cache[K, V]) FetchTTL(key K, f func() (V, time.Duration, error)) (value V, err error)
- func (c *Cache[K, V]) Has(key K) bool
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Load(key K) (value V, loaded bool)
- func (c *Cache[K, V]) Range(f func(key K, value V) bool)
- func (c *Cache[K, V]) Store(key K, value V)
- func (c *Cache[K, V]) StoreTTL(key K, value V, ttl time.Duration)
- type Option
Constants ¶
const ( // FIFO policy orders records in FIFO order. FIFO = backend.FIFO // LFU policy orders records in LFU order. LFU = backend.LFU // LRU policy orders records in LRU order. LRU = backend.LRU )
Available cache eviction policies.
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 in-memory cache.
func (*Cache[K, V]) Fetch ¶
Fetch loads or stores (and initializes) a value for key with the default TTL. If a value exists, f will not be called, otherwise f will be called to fetch the new value. It panics if f panics. Concurrent fetches for the same key will block and return a single result.
func (*Cache[K, V]) FetchTTL ¶
FetchTTL loads or stores (and initializes) a value for key with the specified TTL. If a value exists, f will not be called, otherwise f will be called to fetch the new value. It panics if f panics. Concurrent fetches for the same key will block and return a single result.
func (*Cache[K, V]) Range ¶
Range iterates over initialized cache key-value pairs in no particular order or consistency. If f returns false, range stops the iteration.
f may modify the cache.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option is a cache configuration option.
func WithCapacity ¶
WithCapacity option configures the cache with specified capacity.
The zero values configures unbounded capacity.
func WithPolicy ¶
WithPolicy option configures the cache with specified eviction policy.
The zero value configures the FIFO policy.