Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AnyVersion = versionsinternal.AnyVersion
var NonCachedVersion = versionsinternal.NonCachedVersion
Functions ¶
func WithMapNewWorkerContext ¶ added in v0.7.0
func WithMapNewWorkerContext(fn NewWorkerContext) interface { MapCacheOption ItemCacheOption }
Types ¶
type CacheVersion ¶
type CacheVersion = versionsinternal.CacheVersion
type CachedItem ¶
type CachedItem[V any] struct { // contains filtered or unexported fields }
concurrentcache.CachedMap is an in-memory self-populating single-value cache that deduplicates concurrent requests. Resulting in a sequential execution of the function that generates the cache value.
func NewCachedItem ¶
func NewCachedItem[V any](generateMissingValue ItemGenerator[V], opts ...ItemCacheOption) *CachedItem[V]
func (*CachedItem[V]) Get ¶
func (c *CachedItem[V]) Get(ctx context.Context, minVersion CacheVersion) Result[V]
Get returns the result generated by the Generate function. If the cached value is not older than the provided minVersion, the cached value is returned. If the generator function is already running for the key, Get waits for it to finish. If the context is canceled before the generator function finishes, Get returns a failed result. If all Get calls are canceled before the generator function finishes, the generator function is canceled and the last canceled Get call returns the result generated by the generator function, the value is not cached.
func (*CachedItem[V]) Set ¶ added in v0.4.0
func (c *CachedItem[V]) Set(ctx context.Context, value V, minVersion CacheVersion)
For advanced usecases only. This function allows you to write a value directly to the cache instead of using the Generate function to generate the value. This is useful eg. for initializing the cache with values that are already available/ were stored somewhere. The provided version must not be older than the current version of the cached value otherwise the value will not be set. Version AnyVersion will always set the value, while NonCachedVersion is not allowed and will panic.
type CachedMap ¶
type CachedMap[K comparable, V any] struct { // contains filtered or unexported fields }
concurrentcache.CachedMap is an in-memory self-populating key-addressable cache that deduplicates concurrent requests. Resulting in a sequential execution of the function that generates the cache value.
func NewCachedMap ¶
func NewCachedMap[K comparable, V any](generateMissingValue MapGenerator[K, V], opts ...MapCacheOption) *CachedMap[K, V]
func (*CachedMap[K, V]) Get ¶
func (c *CachedMap[K, V]) Get(ctx context.Context, key K, minVersion CacheVersion) Result[V]
Get returns the result generated by the Generate function for the given key. If the cached value is not older than the provided minVersion, the cached value is returned. If the generator function is already running for the key, Get waits for it to finish. If the context is canceled before the generator function finishes, Get returns a failed result. If all Get calls are canceled before the generator function finishes, the generator function is canceled and the last canceled Get call returns the result generated by the generator function, the value is not cached.
func (*CachedMap[K, V]) Set ¶ added in v0.4.0
func (c *CachedMap[K, V]) Set(key K, value V, minVersion CacheVersion)
For advanced usecases only. This function allows you to write a value directly to the cache instead of using the Generate function to generate the value. This is useful eg. for initializing the cache with values that are already available/ were stored somewhere. The provided version must not be older than the current version of the cached value otherwise the value will not be set. Version AnyVersion will always set the value, while NonCachedVersion is not allowed and will panic.
func (*CachedMap[K, V]) SetAll ¶ added in v0.4.0
func (c *CachedMap[K, V]) SetAll(values map[K]V, minVersion CacheVersion)
For advanced usecases only. This function allows you to write a set of values directly to the cache instead of using the Generate function to generate the value. This is useful eg. for initializing the cache with values that are already available/ were stored somewhere. The provided version must not be older than the current version of the cached value otherwise the value will not be set. Version AnyVersion will always set the value, while NonCachedVersion is not allowed and will panic.
type ItemCacheOption ¶ added in v0.7.0
type ItemCacheOption interface {
// contains filtered or unexported methods
}
type ItemGenerator ¶ added in v0.3.0
ItemGenerator is a function that generates a value for a CachedItem. The function will be called when the cached value is missing or out-of-date. The function can safely access shared resources, without the need for additional synchronization, as it is guaranteed to be called sequentially.
type MapCacheOption ¶ added in v0.7.0
type MapCacheOption interface {
// contains filtered or unexported methods
}
func WithCapacity ¶ added in v0.7.0
func WithCapacity(capacity int) MapCacheOption
Set the capacity of the map cache, any value less than 0 will be ignored.
type MapGenerator ¶ added in v0.3.0
type MapGenerator[K comparable, V any] func(ctx context.Context, key K) (V, error)
MapGenerator is a function that generates a value for a CachedMap. The function will be called when the cached value is missing or out-of-date. The function can safely access shared resources, without the need for additional synchronization, as it is guaranteed to be called sequentially.
type NewWorkerContext ¶ added in v0.5.0
type NewWorkerContext func(getCtxWithoutCancel context.Context) (worker context.Context, getDetached func())
NewWorkerContext is a function that creates a new context for the worker. It receives the context passed to the Get call (without the cancel) and returns the context for the worker and a function that will be called when the Get call is canceled before the worker finishes.
type Result ¶
type Result[V any] struct { // Value is the value that was generated by the worker. Value V // Error is the error that was generated by the worker or // in the case of a canceled Get call, the context cause error. Error error // FromCache is true if the value was returned from the cache instead of // being generated by the worker. FromCache bool // NextVersion is the version that is newer than the version of the result. // If the result is from a canceled Get call, the NextVersion is the version // that was provided as the minVersion argument. NextVersion CacheVersion }