dedupe

package module
v0.0.0-...-ec15e94 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: ISC Imports: 3 Imported by: 0

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

func (c *Cache[K, V]) Check(key K, ttl time.Duration, fn func() (V, error)) (V, error)

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.

func (*Cache[K, V]) Stop

func (c *Cache[K, V]) Stop()

Stops the goroutine processing evictions for this cache.

Jump to

Keyboard shortcuts

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