tools: Index | Files

package memoize

import ""

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

The memoized result is returned again the next time the function is invoked. To prevent excessive memory use, the return values are only remembered for as long as they still have a user.

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


Package Files

memoize.go nocopy.go

type Function Uses

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

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

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() 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) (interface{}, error)

Get returns the value associated with a handle.

If the value is not yet ready, the underlying function will be invoked. This activates the handle, and it will remember the value for as long as it exists. If ctx is cancelled, Get returns nil.

type NoCopy Uses

type NoCopy struct {
    // contains filtered or unexported fields

NoCopy is a type with no public methods that will trigger a vet check if it is ever copied. You can embed this in any type intended to be used as a value. This helps avoid accidentally holding a copy of a value instead of the value itself.

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) Bind Uses

func (s *Store) 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 for as long as the handle is, once it has been generated. Bind does not cause the value to be generated.

func (*Store) Cached Uses

func (s *Store) Cached(key interface{}) interface{}

Cached returns the value associated with a key.

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

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) Find Uses

func (s *Store) Find(key interface{}) *Handle

Find returns the handle associated with a key, if it is bound.

It cannot cause a new handle to be generated, and thus may return nil.

func (*Store) Has Uses

func (s *Store) Has(key interface{}) bool

Has returns true if they key is currently valid for this store.

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 6 packages (graph) and is imported by 3 packages. Updated 2020-07-08. Refresh now. Tools for package owners.