tools: golang.org/x/tools/internal/memoize Index | Files

package memoize

import "golang.org/x/tools/internal/memoize"

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

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.

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{}

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

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

Delete removes a key from the store, if present.

func (*Store) Has Uses

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

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

Package memoize imports 5 packages (graph) and is imported by 2 packages. Updated 2019-07-19. Refresh now. Tools for package owners.