Documentation
¶
Overview ¶
Package dedupe implements a TTL cache designed for de-duplicating the creation of data that has a long-running (and presumably fallible) creation process. The goal of this cache is not performance, but rather a strict guarantee that work is only done once for a given key within a given lifetime.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
func NewCache ¶
func NewCache[K comparable, V any](every time.Duration) *Cache[K, V]
Returns a new Cache instance with the given GC interval.
func NewCacheWithCallback ¶
func NewCacheWithCallback[K comparable, V any](every time.Duration, onEvict func(key K, val V), onEvictCycle func(total, delta int)) *Cache[K, V]
Returns a new Cache instance with the given GC interval and two optional callbacks, the former of which is called every time a key is evicted, and the latter at the completion of every GC cycle.
Note that these callbacks will not be called from the same goroutine that created the Cache instance.
func (*Cache[K, V]) Check ¶
Check if the given key exists within the cache. If yes, the cached result will be returned, otherwise the provided callback will be executed. If the callback returns a nil error then its return value will be cached with the given TTL; in either case the return values of the callback will be the return values of this function.
If this function is called concurrently for the same key from multiple threads their callbacks will be executed in order until the first one is found that does not return an error. The return value from this callback will be cached, and returned as the result of all pending Check calls for that key.