Documentation
¶
Index ¶
- Constants
- type EvictionAlgorithm
- type HotCache
- func (c *HotCache[K, V]) Algorithm() (mainCacheAlgorithm string, missingCacheAlgorithm string)
- func (c *HotCache[K, V]) All() map[K]V
- func (c *HotCache[K, V]) Capacity() (mainCacheCapacity int, missingCacheCapacity int)
- func (c *HotCache[K, V]) Collect(ch chan<- prometheus.Metric)
- 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]) Describe(ch chan<- *prometheus.Desc)
- 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]) WithPrometheusMetrics(cacheName string) 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 ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EvictionAlgorithm ¶ added in v0.2.0
type EvictionAlgorithm string
EvictionAlgorithm represents the cache eviction policy to use.
const ( LRU EvictionAlgorithm = "lru" LFU EvictionAlgorithm = "lfu" TinyLFU EvictionAlgorithm = "tinylfu" WTinyLFU EvictionAlgorithm = "wtinylfu" TwoQueue EvictionAlgorithm = "2q" ARC EvictionAlgorithm = "arc" FIFO EvictionAlgorithm = "fifo" )
type HotCache ¶
type HotCache[K comparable, V any] struct { // contains filtered or unexported fields }
HotCache is the main cache implementation that provides all caching functionality. It supports various eviction policies, TTL, revalidation, and missing key caching.
func (*HotCache[K, V]) Algorithm ¶
Algorithm returns the eviction algorithm names for the main cache and missing cache. If missing cache is shared or not enabled, missingCacheAlgorithm will be empty.
func (*HotCache[K, V]) All ¶ added in v0.9.0
func (c *HotCache[K, V]) All() map[K]V
All returns all key-value pairs in the cache.
func (*HotCache[K, V]) Capacity ¶
Capacity returns the capacity of the main cache and missing cache. If missing cache is shared or not enabled, missingCacheCapacity will be 0.
func (*HotCache[K, V]) Collect ¶ added in v0.7.0
func (c *HotCache[K, V]) Collect(ch chan<- prometheus.Metric)
Collect implements the prometheus.Collector interface.
func (*HotCache[K, V]) Delete ¶
Delete removes a key from the cache. Returns true if the key was found and removed, false otherwise.
func (*HotCache[K, V]) DeleteMany ¶
DeleteMany removes multiple keys from the cache in a single operation. Returns a map where keys are the input keys and values indicate whether the key was found and removed.
func (*HotCache[K, V]) Describe ¶ added in v0.7.0
func (c *HotCache[K, V]) Describe(ch chan<- *prometheus.Desc)
Describe implements the prometheus.Collector interface.
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. Uses the default loaders configured for the cache.
func (*HotCache[K, V]) GetMany ¶
GetMany returns multiple values from the cache, a slice of missing keys, and an error when loaders fail. Uses the default loaders configured for the cache.
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 multiple values from the cache, a slice of missing keys, and an error when loaders fail. Uses the provided loaders for cache misses. Concurrent calls for the same keys are deduplicated using singleflight.
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. Uses the provided loaders for cache misses. Concurrent calls for the same key are deduplicated using singleflight.
func (*HotCache[K, V]) Has ¶
Has checks if a key exists in the cache and has a valid value. Missing values (cached as missing) are not considered valid, even if cached.
func (*HotCache[K, V]) HasMany ¶
HasMany checks if multiple keys exist in the cache and have valid values. Missing values (cached as missing) are not considered valid, even if cached. Returns a map where keys are the input keys and values indicate whether the key exists and has a value.
func (*HotCache[K, V]) Janitor ¶
func (c *HotCache[K, V]) Janitor()
Janitor starts a background goroutine that periodically removes expired items from the cache. The janitor runs until StopJanitor() is called or the cache is garbage collected. This method is safe to call multiple times, but only the first call will start the janitor.
func (*HotCache[K, V]) Keys ¶
func (c *HotCache[K, V]) Keys() []K
Keys returns all keys in the cache that have valid values. Missing keys are not included in the result.
func (*HotCache[K, V]) Len ¶
Len returns the number of items in the main cache. This includes both valid values and missing keys if using shared missing cache.
func (*HotCache[K, V]) MustGet ¶ added in v0.2.0
MustGet returns a value from the cache and a boolean indicating whether the key was found. Panics when loaders fail. Uses the default loaders configured for the cache.
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 multiple values from the cache and a slice of missing keys. Panics when loaders fail. Uses the default loaders configured for the cache.
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 multiple values from the cache and a slice of missing keys. Panics when loaders fail. Uses the provided loaders for cache misses.
func (*HotCache[K, V]) MustGetWithLoaders ¶ added in v0.3.2
MustGetWithLoaders returns a value from the cache and a boolean indicating whether the key was found. Panics when loaders fail. Uses the provided loaders for cache misses.
func (*HotCache[K, V]) Peek ¶
Peek returns a value from the cache without checking expiration or calling loaders/revalidation. Missing values are not returned, even if cached. This is useful for inspection without side effects.
func (*HotCache[K, V]) PeekMany ¶
func (c *HotCache[K, V]) PeekMany(keys []K) (map[K]V, []K)
PeekMany returns multiple values from the cache without checking expiration or calling loaders/revalidation. Missing values are not returned, even if cached. This is useful for inspection without side effects.
func (*HotCache[K, V]) Purge ¶
func (c *HotCache[K, V]) Purge()
Purge removes all keys and values from the cache. This operation clears both the main cache and the missing cache if enabled.
func (*HotCache[K, V]) Range ¶
Range iterates over all key-value pairs in the cache and calls the provided function for each pair. The iteration stops if the function returns false. Missing values are not included. @TODO: loop over missingCache? Use a different callback?
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. Uses the default TTL configured for the cache.
func (*HotCache[K, V]) SetMany ¶
func (c *HotCache[K, V]) SetMany(items map[K]V)
SetMany adds multiple values to the cache in a single operation. If keys already exist, their values are updated. Uses the default TTL configured for the cache.
func (*HotCache[K, V]) SetManyWithTTL ¶
SetManyWithTTL adds multiple values to the cache with a specific TTL duration. If keys already exist, their values are updated.
func (*HotCache[K, V]) SetMissing ¶
func (c *HotCache[K, V]) SetMissing(key K)
SetMissing adds a key to the missing cache to prevent repeated lookups for non-existent keys. If the key already exists, its value is dropped. Uses the default TTL configured for the cache. Panics if missing cache is not enabled.
func (*HotCache[K, V]) SetMissingMany ¶
func (c *HotCache[K, V]) SetMissingMany(missingKeys []K)
SetMissingMany adds multiple keys to the missing cache in a single operation. If keys already exist, their values are dropped. Uses the default TTL configured for the cache. Panics if missing cache is not enabled.
func (*HotCache[K, V]) SetMissingManyWithTTL ¶
SetMissingManyWithTTL adds multiple keys to the missing cache with a specific TTL duration. If keys already exist, their values are dropped. Panics if missing cache is not enabled.
func (*HotCache[K, V]) SetMissingWithTTL ¶
SetMissingWithTTL adds a key to the missing cache with a specific TTL duration. If the key already exists, its value is dropped. Panics if missing cache is not enabled.
func (*HotCache[K, V]) SetWithTTL ¶
SetWithTTL adds a value to the cache with a specific TTL duration. If the key already exists, its value is updated.
func (*HotCache[K, V]) StopJanitor ¶
func (c *HotCache[K, V]) StopJanitor()
StopJanitor stops the background janitor goroutine and cleans up resources. This method is safe to call multiple times and will wait for the janitor to fully stop.
func (*HotCache[K, V]) Values ¶
func (c *HotCache[K, V]) Values() []V
Values returns all values in the cache. Missing values are not included in the result.
type HotCacheConfig ¶
type HotCacheConfig[K comparable, V any] struct { // contains filtered or unexported fields }
HotCacheConfig holds the configuration for a HotCache instance. It uses the builder pattern to allow fluent configuration.
func NewHotCache ¶
func NewHotCache[K comparable, V any](algorithm EvictionAlgorithm, capacity int) HotCacheConfig[K, V]
NewHotCache creates a new HotCache configuration with the specified eviction algorithm and capacity. This is the starting point for building a cache with the builder pattern.
func (HotCacheConfig[K, V]) Build ¶
func (cfg HotCacheConfig[K, V]) Build() *HotCache[K, V]
Build creates and returns a new HotCache instance with the current configuration. This method validates the configuration and creates all necessary internal components. The cache is ready to use immediately after this call.
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 when reading from the cache. This is useful for ensuring thread safety when the cached values are mutable.
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 when writing to the cache. This is useful for ensuring thread safety when the cached values are mutable.
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 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 that periodically removes expired items. The janitor runs in the background and cannot be used together with WithoutLocking().
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). This helps prevent cache stampedes by spreading out when entries expire.
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. These loaders will be called in sequence when a key is not found in the cache.
func (HotCacheConfig[K, V]) WithMissingCache ¶
func (cfg HotCacheConfig[K, V]) WithMissingCache(algorithm EvictionAlgorithm, capacity int) HotCacheConfig[K, V]
WithMissingCache enables caching of missing keys in a separate cache instance. The missing keys are stored in a dedicated cache with its own eviction algorithm and capacity.
func (HotCacheConfig[K, V]) WithMissingSharedCache ¶
func (cfg HotCacheConfig[K, V]) WithMissingSharedCache() HotCacheConfig[K, V]
WithMissingSharedCache enables caching of missing keys in the main cache. Missing keys are stored alongside regular values in the same cache instance.
func (HotCacheConfig[K, V]) WithPrometheusMetrics ¶ added in v0.7.0
func (cfg HotCacheConfig[K, V]) WithPrometheusMetrics(cacheName string) HotCacheConfig[K, V]
WithPrometheusMetrics enables metric collection for the cache with the specified name. The cache name is required when metrics are enabled and will be used as a label in Prometheus metrics. When the cache is sharded, metrics will be collected for each shard with the shard number as an additional label.
func (HotCacheConfig[K, V]) WithRevalidation ¶
func (cfg HotCacheConfig[K, V]) WithRevalidation(stale time.Duration, loaders ...Loader[K, V]) HotCacheConfig[K, V]
WithRevalidation sets the stale duration and optional revalidation loaders. After the TTL expires, entries become stale and can still be served while being revalidated in the background. Keys that are not fetched during the stale period will be dropped. If no revalidation loaders are provided, the default loaders or those 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, keys are dropped from the cache on revalidation errors.
func (HotCacheConfig[K, V]) WithSharding ¶
func (cfg HotCacheConfig[K, V]) WithSharding(nbr uint64, fn sharded.Hasher[K]) HotCacheConfig[K, V]
WithSharding enables cache sharding for better concurrency performance. The cache is split into multiple shards based on the provided hash function.
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. After this duration, entries will be considered expired and will be removed.
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 data from the provided function. This is useful for initializing the cache with frequently accessed 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 data from the provided function with a timeout. This is useful when the inner callback does not have its own 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 performance. This should only be used when the cache is not accessed concurrently. Cannot be used together with WithJanitor().
type Loader ¶
type Loader[K comparable, V any] func(keys []K) (found map[K]V, err error)
Loader is a function type that loads values for the given keys. It should return a map of found key-value pairs and an error if the operation fails. Keys that cannot be found should not be included in the returned map.
type LoaderChain ¶
type LoaderChain[K comparable, V any] []Loader[K, V]
LoaderChain is a slice of loaders that are executed in sequence. Each loader is called with the keys that were not found by previous loaders.