memoize

package
v0.0.0-...-57c1bf3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2019 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Overview

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

The memoizied 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 aquire handles with the Bind method.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Function

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

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.

func (*Handle) Cached

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

func (h *Handle) Get(ctx context.Context) interface{}

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. This will cause any other handles for the same key to also return the same value.

type NoCopy

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

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

func (s *Store) Bind(key interface{}, function Function) *Handle

Bind returns a handle for the given key and function.

Each call to bind will generate a new handle. All of of the handles for a single key will refer to the same value. Only the first handle to get the value will cause the function to be invoked. The value will be held for as long as there are handles through which it has been accessed. Bind does not cause the value to be generated.

func (*Store) Cached

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) Delete

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

Delete removes a key from the store, if present.

func (*Store) Has

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

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

Jump to

Keyboard shortcuts

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