Documentation
¶
Index ¶
- Variables
- type CacheAlgorithm
- type HotCache
- func (c *HotCache[K, V]) Algorithm() (string, string)
- func (c *HotCache[K, V]) Capacity() (int, int)
- func (c *HotCache[K, V]) Delete(key K) bool
- func (c *HotCache[K, V]) DeleteMany(keys []K) map[K]bool
- func (c *HotCache[K, V]) Get(key K) (value V, ok bool, err error)
- func (c *HotCache[K, V]) GetMany(keys []K) (map[K]V, []K, error)
- func (c *HotCache[K, V]) GetManyWithCustomLoaders(keys []K, customLoaders LoaderChain[K, V]) (map[K]V, []K, error)
- func (c *HotCache[K, V]) GetWithCustomLoaders(key K, customLoaders LoaderChain[K, V]) (value V, ok bool, err error)
- func (c *HotCache[K, V]) Has(key K) bool
- func (c *HotCache[K, V]) HasMany(keys []K) map[K]bool
- func (c *HotCache[K, V]) Janitor()
- func (c *HotCache[K, V]) Keys() []K
- func (c *HotCache[K, V]) Len() int
- func (c *HotCache[K, V]) Peek(key K) (value V, ok bool)
- func (c *HotCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
- func (c *HotCache[K, V]) Purge()
- func (c *HotCache[K, V]) Range(f func(K, V) bool)
- func (c *HotCache[K, V]) Set(key K, v V)
- func (c *HotCache[K, V]) SetMany(items map[K]V)
- func (c *HotCache[K, V]) SetManyWithTTL(items map[K]V, ttl time.Duration)
- func (c *HotCache[K, V]) SetMissing(key K)
- func (c *HotCache[K, V]) SetMissingMany(missingKeys []K)
- func (c *HotCache[K, V]) SetMissingManyWithTTL(missingKeys []K, ttl time.Duration)
- func (c *HotCache[K, V]) SetMissingWithTTL(key K, ttl time.Duration)
- func (c *HotCache[K, V]) SetWithTTL(key K, v V, ttl time.Duration)
- func (c *HotCache[K, V]) StopJanitor()
- func (c *HotCache[K, V]) Values() []V
- func (c *HotCache[K, V]) WarmUp(loader func() (map[K]V, []K, error)) error
- type HotCacheConfig
- func (cfg HotCacheConfig[K, V]) Build() *HotCache[K, V]
- func (cfg HotCacheConfig[K, V]) WithCopyOnRead(copyOnRead func(V) V) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithCopyOnWrite(copyOnWrite func(V) V) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithJanitor() HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithJitter(jitter float64) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithLoaders(loaders ...Loader[K, V]) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithMissingCache(algorithm CacheAlgorithm, capacity int) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithMissingSharedCache() HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithRevalidation(stale time.Duration, loaders ...Loader[K, V]) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithSharding(nbr uint64, fn sharded.Hasher[K]) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithTTL(ttl time.Duration) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithWarmUp(fn func() (map[K]V, []K, error)) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithoutLocking() HotCacheConfig[K, V]
- type Loader
- type LoaderChain
- type Metrics
Constants ¶
This section is empty.
Variables ¶
var DebounceRevalidationFactor = 0.2
Revalidation is done in batch,.
Functions ¶
This section is empty.
Types ¶
type HotCache ¶
type HotCache[K comparable, V any] struct { // contains filtered or unexported fields }
func (*HotCache[K, V]) DeleteMany ¶
DeleteMany removes many keys from the cache.
func (*HotCache[K, V]) Get ¶
Get returns a value from the cache, a boolean indicating whether the key was found and an error when loaders fail.
func (*HotCache[K, V]) GetMany ¶
GetMany returns many values from the cache, a slice of missing keys and an error when loaders fail.
func (*HotCache[K, V]) GetManyWithCustomLoaders ¶
func (c *HotCache[K, V]) GetManyWithCustomLoaders(keys []K, customLoaders LoaderChain[K, V]) (map[K]V, []K, error)
GetManyWithCustomLoaders returns many values from the cache, a slice of missing keys and an error when loaders fail.
func (*HotCache[K, V]) GetWithCustomLoaders ¶
func (c *HotCache[K, V]) GetWithCustomLoaders(key K, customLoaders LoaderChain[K, V]) (value V, ok bool, err error)
GetWithCustomLoaders returns a value from the cache, a boolean indicating whether the key was found and an error when loaders fail.
func (*HotCache[K, V]) Has ¶
Has checks if a key exists in the cache. Missing values are not valid, even if cached.
func (*HotCache[K, V]) HasMany ¶
HasMany checks if keys exist in the cache. Missing values are not valid, even if cached.
func (*HotCache[K, V]) Janitor ¶
func (c *HotCache[K, V]) Janitor()
Janitor runs a background goroutine to clean up the cache.
func (*HotCache[K, V]) Keys ¶
func (c *HotCache[K, V]) Keys() []K
Keys returns all keys in the cache. Missing keys are not included.
func (*HotCache[K, V]) Len ¶
Len returns the number of items in the cache. Missing items are included.
func (*HotCache[K, V]) Peek ¶
Peek is similar to Get, but do not check expiration and do not call loaders/revalidation. Missing values are not returned, even if cached.
func (*HotCache[K, V]) PeekMany ¶
func (c *HotCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
PeekMany is similar to GetMany, but do not check expiration and do not call loaders/revalidation. Missing values are not returned, even if cached.
func (*HotCache[K, V]) Purge ¶
func (c *HotCache[K, V]) Purge()
Purge removes all items from the cache.
func (*HotCache[K, V]) Range ¶
Range calls a function for each key/value pair in the cache. The callback should be kept short because it is called while holding a read lock. @TODO: loop over missingCache? Use a different callback? Missing values are not included.
func (*HotCache[K, V]) Set ¶
func (c *HotCache[K, V]) Set(key K, v V)
Set adds a value to the cache. If the key already exists, its value is updated. It uses the default ttl or none.
func (*HotCache[K, V]) SetMany ¶
func (c *HotCache[K, V]) SetMany(items map[K]V)
SetMany adds many values to the cache. If the keys already exist, values are updated. It uses the default ttl or none.
func (*HotCache[K, V]) SetManyWithTTL ¶
SetManyWithTTL adds many values to the cache. If the keys already exist, values are updated. It uses the given ttl.
func (*HotCache[K, V]) SetMissing ¶
func (c *HotCache[K, V]) SetMissing(key K)
SetMissing adds a key to the `missing` cache. If the key already exists, its value is dropped. It uses the default ttl or none.
func (*HotCache[K, V]) SetMissingMany ¶
func (c *HotCache[K, V]) SetMissingMany(missingKeys []K)
SetMissingMany adds many keys to the cache. If the keys already exist, values are dropped. It uses the default ttl or none.
func (*HotCache[K, V]) SetMissingManyWithTTL ¶
SetManyWithTTL adds many keys to the cache. If the keys already exist, values are dropped. It uses the given ttl.
func (*HotCache[K, V]) SetMissingWithTTL ¶
SetMissingWithTTL adds a key to the `missing` cache. If the key already exists, its value is dropped. It uses the given ttl.
func (*HotCache[K, V]) SetWithTTL ¶
SetWithTTL adds a value to the cache. If the key already exists, its value is updated. It uses the given ttl.
func (*HotCache[K, V]) StopJanitor ¶
func (c *HotCache[K, V]) StopJanitor()
type HotCacheConfig ¶
type HotCacheConfig[K comparable, V any] struct { // contains filtered or unexported fields }
func NewHotCache ¶
func NewHotCache[K comparable, V any](algorithm CacheAlgorithm, capacity int) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) Build ¶
func (cfg HotCacheConfig[K, V]) Build() *HotCache[K, V]
func (HotCacheConfig[K, V]) WithCopyOnRead ¶
func (cfg HotCacheConfig[K, V]) WithCopyOnRead(copyOnRead func(V) V) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithCopyOnWrite ¶
func (cfg HotCacheConfig[K, V]) WithCopyOnWrite(copyOnWrite func(V) V) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithJanitor ¶
func (cfg HotCacheConfig[K, V]) WithJanitor() HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithJitter ¶
func (cfg HotCacheConfig[K, V]) WithJitter(jitter float64) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithLoaders ¶
func (cfg HotCacheConfig[K, V]) WithLoaders(loaders ...Loader[K, V]) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithMissingCache ¶
func (cfg HotCacheConfig[K, V]) WithMissingCache(algorithm CacheAlgorithm, capacity int) HotCacheConfig[K, V]
WithMissingCache enables cache of missing keys. The missing keys are stored in a separate cache.
func (HotCacheConfig[K, V]) WithMissingSharedCache ¶
func (cfg HotCacheConfig[K, V]) WithMissingSharedCache() HotCacheConfig[K, V]
WithMissingSharedCache enables cache of missing keys. The missing cache is shared with the main cache.
func (HotCacheConfig[K, V]) WithRevalidation ¶
func (cfg HotCacheConfig[K, V]) WithRevalidation(stale time.Duration, loaders ...Loader[K, V]) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithSharding ¶
func (cfg HotCacheConfig[K, V]) WithSharding(nbr uint64, fn sharded.Hasher[K]) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithTTL ¶
func (cfg HotCacheConfig[K, V]) WithTTL(ttl time.Duration) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithWarmUp ¶
func (cfg HotCacheConfig[K, V]) WithWarmUp(fn func() (map[K]V, []K, error)) HotCacheConfig[K, V]
func (HotCacheConfig[K, V]) WithoutLocking ¶
func (cfg HotCacheConfig[K, V]) WithoutLocking() HotCacheConfig[K, V]
type Loader ¶
type Loader[K comparable, V any] func(keys []K) (found map[K]V, err error)
type LoaderChain ¶
type LoaderChain[K comparable, V any] []Loader[K, V]
type Metrics ¶
type Metrics struct {
Insertion prometheus.Counter
Eviction prometheus.Counter
Hit prometheus.Counter
Miss prometheus.Counter
Length prometheus.Gauge
Weight prometheus.Gauge
// settings
// SettingsCapacity prometheus.Gauge
SettingsTTL prometheus.Gauge
SettingsJitter prometheus.Gauge
SettingsStale prometheus.Gauge
// SettingsMissingCapacity prometheus.Gauge
SettingsMissingTTL prometheus.Gauge
SettingsMissingStale prometheus.Gauge
}
@TODO: Should be simple atomic counters and gauges. @TODO: If metrics are disabled, no need to collect them (use a no-op implementation). @TODO: If prometheus metrics are enabled, collect them in a separate goroutine. @TODO: collect revalidation count+delay @TODO: add a label for the cache name @TODO: add comment to metric declaration @TODO: weight should be diplicated in order to include *item[V] weight