cache

package
v1.108.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheGroup

type CacheGroup[T comparable] struct {
	// contains filtered or unexported fields
}

CacheGroup provides single flighting for grouping repeated calls for the same workload, as well as a cache that extends the lifetime of the returned result by a specific duration.

func NewCacheGroup

func NewCacheGroup[T comparable](max int) *CacheGroup[T]

NewCacheGroup[T] creates a new cache group instance given the max number of keys to cache. If a new cache entry is added that exceeds the maximum, the oldest entry is evicted.

func (*CacheGroup[T]) DisableExpiration

func (cg *CacheGroup[T]) DisableExpiration()

DisableExpiration will shutdown the expiration process which allows cache entries to remain until 'max' is exceeded.

func (*CacheGroup[T]) Do

func (cg *CacheGroup[T]) Do(key string, factory func() (T, error)) (T, error)

Do accepts a group key and a factory function to execute a workload request. Any executions of Do() using an identical key will wait on the originating request rather than executing a new request, and the final result will be shared among any callers sharing the same key. Additionally, once returned, the workload for that key will remained cached. An expiration policy can be added for this cache by calling the WithExpiration method.

func (*CacheGroup[T]) WithExpiration

func (cg *CacheGroup[T]) WithExpiration(expiry time.Duration, evictionInterval time.Duration) *CacheGroup[T]

WithExpiration assigns a cache expiration to cached entries, and starts an eviction process, which runs on the specified interval.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL