Documentation
¶
Index ¶
- Constants
- Variables
- type EvictionAlgorithm
- 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, found bool, err error)
- func (c *HotCache[K, V]) GetMany(keys []K) (values map[K]V, missing []K, err error)
- func (c *HotCache[K, V]) GetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K, err error)
- func (c *HotCache[K, V]) GetWithLoaders(key K, loaders ...Loader[K, V]) (value V, found 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]) MustGet(key K) (value V, found bool)
- func (c *HotCache[K, V]) MustGetMany(keys []K) (values map[K]V, missing []K)
- func (c *HotCache[K, V]) MustGetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K)
- func (c *HotCache[K, V]) MustGetWithLoaders(key K, loaders ...Loader[K, V]) (value V, found bool)
- 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]) WithEvictionCallback(onEviction base.EvictionCallback[K, V]) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithJanitor() HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithJitter(lambda float64, upperBoundDuration time.Duration) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithLoaders(loaders ...Loader[K, V]) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithMissingCache(algorithm EvictionAlgorithm, 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]) WithRevalidationErrorPolicy(policy revalidationErrorPolicy) 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]) WithWarmUpWithTimeout(timeout time.Duration, fn func() (map[K]V, []K, error)) HotCacheConfig[K, V]
- func (cfg HotCacheConfig[K, V]) WithoutLocking() HotCacheConfig[K, V]
- type Loader
- type LoaderChain
Constants ¶
const ( DropOnError revalidationErrorPolicy = iota KeepOnError )
Variables ¶
var DebounceRevalidationFactor = 0.2
Revalidation is done in batch,.
Functions ¶
This section is empty.
Types ¶
type EvictionAlgorithm ¶ added in v0.2.0
type EvictionAlgorithm int
const ( LRU EvictionAlgorithm = iota LFU TwoQueue ARC )
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]) GetManyWithLoaders ¶ added in v0.3.2
func (c *HotCache[K, V]) GetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K, err error)
GetManyWithLoaders returns many values from the cache, a slice of missing keys and an error when loaders fail.
func (*HotCache[K, V]) GetWithLoaders ¶ added in v0.3.2
func (c *HotCache[K, V]) GetWithLoaders(key K, loaders ...Loader[K, V]) (value V, found bool, err error)
GetWithLoaders 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]) MustGet ¶ added in v0.2.0
MustGet returns a value from the cache, a boolean indicating whether the key was found. It panics when loaders fail.
func (*HotCache[K, V]) MustGetMany ¶ added in v0.2.0
func (c *HotCache[K, V]) MustGetMany(keys []K) (values map[K]V, missing []K)
MustGetMany returns many values from the cache, a slice of missing keys. It panics when loaders fail.
func (*HotCache[K, V]) MustGetManyWithLoaders ¶ added in v0.3.2
func (c *HotCache[K, V]) MustGetManyWithLoaders(keys []K, loaders ...Loader[K, V]) (values map[K]V, missing []K)
MustGetManyWithLoaders returns many values from the cache, a slice of missing keys. It panics when loaders fail.
func (*HotCache[K, V]) MustGetWithLoaders ¶ added in v0.3.2
MustGetWithLoaders returns a value from the cache, a boolean indicating whether the key was found. It panics when loaders fail.
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 EvictionAlgorithm, 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]
WithCopyOnRead sets the function to copy the value on read.
func (HotCacheConfig[K, V]) WithCopyOnWrite ¶
func (cfg HotCacheConfig[K, V]) WithCopyOnWrite(copyOnWrite func(V) V) HotCacheConfig[K, V]
WithCopyOnWrite sets the function to copy the value on write.
func (HotCacheConfig[K, V]) WithEvictionCallback ¶ added in v0.2.0
func (cfg HotCacheConfig[K, V]) WithEvictionCallback(onEviction base.EvictionCallback[K, V]) HotCacheConfig[K, V]
WithEvictionCallback sets the callback to be called when an entry is evicted from the cache. The callback is called synchronously and might block the cache operations if it is slow. This implementation choice is subject to change. Please open an issue to discuss.
func (HotCacheConfig[K, V]) WithJanitor ¶
func (cfg HotCacheConfig[K, V]) WithJanitor() HotCacheConfig[K, V]
WithJanitor enables the cache janitor.
func (HotCacheConfig[K, V]) WithJitter ¶
func (cfg HotCacheConfig[K, V]) WithJitter(lambda float64, upperBoundDuration time.Duration) HotCacheConfig[K, V]
WithJitter randomizes the TTL with an exponential distribution in the range [0, +upperBoundDuration).
func (HotCacheConfig[K, V]) WithLoaders ¶
func (cfg HotCacheConfig[K, V]) WithLoaders(loaders ...Loader[K, V]) HotCacheConfig[K, V]
WithLoaders sets the chain of loaders to use for cache misses.
func (HotCacheConfig[K, V]) WithMissingCache ¶
func (cfg HotCacheConfig[K, V]) WithMissingCache(algorithm EvictionAlgorithm, 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]
WithRevalidation sets the time after which the cache entry is considered stale and needs to be revalidated. Keys that are not fetched during the interval will be dropped anyway. A timeout or error in loader will drop keys. If no revalidation loader is added, the default loaders or the one used in GetWithLoaders() are used.
func (HotCacheConfig[K, V]) WithRevalidationErrorPolicy ¶ added in v0.2.0
func (cfg HotCacheConfig[K, V]) WithRevalidationErrorPolicy(policy revalidationErrorPolicy) HotCacheConfig[K, V]
WithRevalidationErrorPolicy sets the policy to apply when a revalidation loader returns an error. By default, the key is dropped from the cache.
func (HotCacheConfig[K, V]) WithSharding ¶
func (cfg HotCacheConfig[K, V]) WithSharding(nbr uint64, fn sharded.Hasher[K]) HotCacheConfig[K, V]
WithSharding enables cache sharding.
func (HotCacheConfig[K, V]) WithTTL ¶
func (cfg HotCacheConfig[K, V]) WithTTL(ttl time.Duration) HotCacheConfig[K, V]
WithTTL sets the time-to-live for cache entries.
func (HotCacheConfig[K, V]) WithWarmUp ¶
func (cfg HotCacheConfig[K, V]) WithWarmUp(fn func() (map[K]V, []K, error)) HotCacheConfig[K, V]
WithWarmUp preloads the cache with the provided data.
func (HotCacheConfig[K, V]) WithWarmUpWithTimeout ¶ added in v0.6.0
func (cfg HotCacheConfig[K, V]) WithWarmUpWithTimeout(timeout time.Duration, fn func() (map[K]V, []K, error)) HotCacheConfig[K, V]
WithWarmUpWithTimeout preloads the cache with the provided data. It can be used when the inner callback does not have timeout strategy.
func (HotCacheConfig[K, V]) WithoutLocking ¶
func (cfg HotCacheConfig[K, V]) WithoutLocking() HotCacheConfig[K, V]
WithoutLocking disables mutex for the cache and improves internal performances.
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]