tools: Index | Files

package memoize

import ""

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.


Package Files


type Arg Uses

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 Uses

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 Uses

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 Uses

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 Uses

func (g *Generation) Bind(key interface{}, function Function) *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.

func (*Generation) Destroy Uses

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 Uses

func (g *Generation) Inherit(h *Handle)

type Handle Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

Package memoize imports 7 packages (graph) and is imported by 4 packages. Updated 2020-09-19. Refresh now. Tools for package owners.