memoize

package
v0.0.0-...-f39ad0c Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: GPL-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package memoize supports memoizing the return values of functions with idempotent results that are expensive to compute.

To use this package, build a store and use it to acquire handles with the Bind method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Arg

type Arg interface {
	// contains filtered or unexported methods
}

Arg is a marker interface that can be embedded to indicate a type is intended for use as a Function argument.

type Function

type Function func(ctx context.Context, arg Arg) interface{}

Function is the type for functions that can be memoized. The result must be a pointer.

type Generation

type Generation struct {
	// contains filtered or unexported fields
}

A Generation is a logical point in time of the cache life-cycle. Cache entries associated with a Generation will not be removed until the Generation is destroyed.

func (*Generation) Acquire

func (g *Generation) Acquire(ctx context.Context) func()

Acquire creates a new reference to g, and returns a func to release that reference.

func (*Generation) Bind

func (g *Generation) Bind(key interface{}, function Function, cleanup func(interface{})) *Handle

Bind returns a handle for the given key and function.

Each call to bind will return the same handle if it is already bound. Bind will always return a valid handle, creating one if needed. Each key can only have one handle at any given time. The value will be held at least until the associated generation is destroyed. Bind does not cause the value to be generated.

If cleanup is non-nil, it will be called on any non-nil values produced by function when they are no longer referenced.

func (*Generation) Destroy

func (g *Generation) Destroy()

Destroy waits for all operations referencing g to complete, then removes all references to g from cache entries. Cache entries that no longer reference any non-destroyed generation are removed. Destroy must be called exactly once for each generation.

func (*Generation) Inherit

func (g *Generation) Inherit(hs ...*Handle)

type Handle

type Handle struct {
	// contains filtered or unexported fields
}

Handle is returned from a store when a key is bound to a function. It is then used to access the results of that function.

A Handle starts out in idle state, waiting for something to demand its evaluation. It then transitions into running state. While it's running, waiters tracks the number of Get calls waiting for a result, and the done channel is used to notify waiters of the next state transition. Once the evaluation finishes, value is set, state changes to completed, and done is closed, unblocking waiters. Alternatively, as Get calls are cancelled, they decrement waiters. If it drops to zero, the inner context is cancelled, computation is abandoned, and state resets to idle to start the process over again.

func (*Handle) Cached

func (h *Handle) Cached(g *Generation) interface{}

Cached returns the value associated with a handle.

It will never cause the value to be generated. It will return the cached value, if present.

func (*Handle) Get

func (h *Handle) Get(ctx context.Context, g *Generation, arg Arg) (interface{}, error)

Get returns the value associated with a handle.

If the value is not yet ready, the underlying function will be invoked. If ctx is cancelled, Get returns nil.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store binds keys to functions, returning handles that can be used to access the functions results.

func (*Store) DebugOnlyIterate

func (s *Store) DebugOnlyIterate(f func(k, v interface{}))

DebugOnlyIterate iterates through all live cache entries and calls f on them. It should only be used for debugging purposes.

func (*Store) Generation

func (s *Store) Generation(name string) *Generation

Generation creates a new Generation associated with s. Destroy must be called on the returned Generation once it is no longer in use. name is for debugging purposes only.

func (*Store) Stats

func (s *Store) Stats() map[reflect.Type]int

Stats returns the number of each type of value in the store.

Jump to

Keyboard shortcuts

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