Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Auto ¶
func Auto[T any](f T, opts ...MemoizerOption) T
Auto returns a memoized version of the function f By default it uses MapCache as the cache and Fn64aKeyer as the KeyerFunc.
Example ¶
i := 0 expensiveFunc := func(x int) { i += x } memoized := Auto(expensiveFunc) // memoize.Auto memoized(100) memoized(1) memoized(1) fmt.Println(i)
Output: 101
func Manual ¶
func Manual[T any, V comparable](f T, opts ...MemoizerOption) func(V) T
Manual returns a function F(V) T that returns the memoized function f. The key needs to be explicitly be passed to F(V).
Example ¶
i := 0 expensiveFunc := func(x int) { i += x } memoized := Manual[func(int), string](expensiveFunc) // memoize.Manual memoized("key1")(100) memoized("key2")(1) memoized("key2")(1) fmt.Println(i)
Output: 101
Types ¶
type Cache ¶
type Cache interface { // Get returns the cached result for the given args Get(args []reflect.Value) ([]reflect.Value, bool) // Set associates the given args with the given res Set(args []reflect.Value, res []reflect.Value) // Clear clears the cache Clear() // Len returns the number of cached argument calls Len() int }
Cache is the interface needed to be satisfied by Cache implementations. Implementations needs be safe for concurrent use.
type MapCache ¶
type MapCache[T comparable] struct { sync.RWMutex // contains filtered or unexported fields }
MapCache is a cache that uses a map to store the results
func NewMapCache ¶
func NewMapCache[T comparable](keyer KeyerFunc[T], opts ...MapCacheOption) *MapCache[T]
type MapCacheOption ¶
MapCacheOption is an option that can be set to a MapCache
func WithCleanDur ¶
func WithCleanDur(ctx context.Context, dur time.Duration) MapCacheOption
WithCleanDur sets the duration between when the cache is cleared
type MemoizerOption ¶
type MemoizerOption func(*memoizer[any])
MemoizerOption is a function that sets an option on the memoizer
Click to show internal directories.
Click to hide internal directories.