Documentation
¶
Overview ¶
Package memoize provides bounded, concurrent memoization for operations.
Index ¶
- Variables
- type Func
- type Func2
- type Func3
- func (o Func3[A, B, C, V]) Call(ctx context.Context, a A, b B, c C) (V, error)
- func (o Func3[A, B, C, V]) Clear()
- func (o Func3[A, B, C, V]) Forget(a A, b B, c C)
- func (o Func3[A, B, C, V]) Name() string
- func (o Func3[A, B, C, V]) Refresh(ctx context.Context, a A, b B, c C) (V, error)
- func (o Func3[A, B, C, V]) Stats() Stats
- type Func4
- func (o Func4[A, B, C, D, V]) Call(ctx context.Context, a A, b B, c C, d D) (V, error)
- func (o Func4[A, B, C, D, V]) Clear()
- func (o Func4[A, B, C, D, V]) Forget(a A, b B, c C, d D)
- func (o Func4[A, B, C, D, V]) Name() string
- func (o Func4[A, B, C, D, V]) Refresh(ctx context.Context, a A, b B, c C, d D) (V, error)
- func (o Func4[A, B, C, D, V]) Stats() Stats
- type Memoizer
- func (m *Memoizer) Func[A comparable, V any](load func(context.Context, A) (V, error), options ...Option) Func[A, V]
- func (m *Memoizer) Func2[A, B comparable, V any](load func(context.Context, A, B) (V, error), options ...Option) Func2[A, B, V]
- func (m *Memoizer) Func2By[A, B any, K comparable, V any](load func(context.Context, A, B) (V, error), key func(A, B) K, ...) Func2[A, B, V]
- func (m *Memoizer) Func3[A, B, C comparable, V any](load func(context.Context, A, B, C) (V, error), options ...Option) Func3[A, B, C, V]
- func (m *Memoizer) Func3By[A, B, C any, K comparable, V any](load func(context.Context, A, B, C) (V, error), key func(A, B, C) K, ...) Func3[A, B, C, V]
- func (m *Memoizer) Func4[A, B, C, D comparable, V any](load func(context.Context, A, B, C, D) (V, error), options ...Option) Func4[A, B, C, D, V]
- func (m *Memoizer) Func4By[A, B, C, D any, K comparable, V any](load func(context.Context, A, B, C, D) (V, error), key func(A, B, C, D) K, ...) Func4[A, B, C, D, V]
- func (m *Memoizer) FuncBy[A any, K comparable, V any](load func(context.Context, A) (V, error), key func(A) K, options ...Option) Func[A, V]
- func (m *Memoizer) Once[V any](load func(context.Context) (V, error), options ...Option) Once[V]
- type Once
- type Option
- type PanicError
- type Policy
- type Stats
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLoaderGoexit is returned when a loader terminates its goroutine with // runtime.Goexit. ErrLoaderGoexit = errors.New("memoize: loader called runtime.Goexit") // ErrRecursiveLoad is returned when a load waits on a flight already in its // propagated loader-context chain. ErrRecursiveLoad = errors.New("memoize: recursive load") )
Functions ¶
This section is empty.
Types ¶
type Func ¶
type Func[A, V any] struct { // contains filtered or unexported fields }
Func is a memoized one-argument operation.
func (Func[A, V]) Clear ¶
func (o Func[A, V]) Clear()
Clear invalidates every result and in-progress generation.
type Func2 ¶
type Func2[A, B, V any] struct { // contains filtered or unexported fields }
Func2 is a memoized two-argument operation.
func (Func2[A, B, V]) Clear ¶
func (o Func2[A, B, V]) Clear()
Clear invalidates every result and in-progress generation.
func (Func2[A, B, V]) Forget ¶
func (o Func2[A, B, V]) Forget(a A, b B)
Forget invalidates the result for the arguments.
type Func3 ¶
type Func3[A, B, C, V any] struct { // contains filtered or unexported fields }
Func3 is a memoized three-argument operation.
func (Func3[A, B, C, V]) Clear ¶
func (o Func3[A, B, C, V]) Clear()
Clear invalidates every result and in-progress generation.
func (Func3[A, B, C, V]) Forget ¶
func (o Func3[A, B, C, V]) Forget(a A, b B, c C)
Forget invalidates the result for the arguments.
type Func4 ¶
type Func4[A, B, C, D, V any] struct { // contains filtered or unexported fields }
Func4 is a memoized four-argument operation.
func (Func4[A, B, C, D, V]) Clear ¶
func (o Func4[A, B, C, D, V]) Clear()
Clear invalidates every result and in-progress generation.
func (Func4[A, B, C, D, V]) Forget ¶
func (o Func4[A, B, C, D, V]) Forget(a A, b B, c C, d D)
Forget invalidates the result for the arguments.
type Memoizer ¶
type Memoizer struct {
// contains filtered or unexported fields
}
Memoizer constructs independently bounded typed operations.
A Memoizer must be constructed with New or MustNew. Constructed values may be copied.
Example ¶
memo := MustNew(Policy{TTL: time.Minute, Capacity: 100})
var calls int
findUser := memo.Func2(func(_ context.Context, tenant, id string) (user, error) {
calls++
return user{ID: id, Name: tenant + ":Ada"}, nil
}, Named("users.find"))
first, _ := findUser.Call(context.Background(), "acme", "42")
second, _ := findUser.Call(context.Background(), "acme", "42")
fmt.Println(first.Name, second.Name, calls)
Output: acme:Ada acme:Ada 1
func (*Memoizer) Func ¶
func (m *Memoizer) Func[A comparable, V any]( load func(context.Context, A) (V, error), options ...Option, ) Func[A, V]
Func creates an operation keyed by its argument.
func (*Memoizer) Func2 ¶
func (m *Memoizer) Func2[A, B comparable, V any]( load func(context.Context, A, B) (V, error), options ...Option, ) Func2[A, B, V]
Func2 creates an operation keyed by its arguments.
func (*Memoizer) Func2By ¶
func (m *Memoizer) Func2By[A, B any, K comparable, V any]( load func(context.Context, A, B) (V, error), key func(A, B) K, options ...Option, ) Func2[A, B, V]
Func2By creates an operation keyed by key.
func (*Memoizer) Func3 ¶
func (m *Memoizer) Func3[A, B, C comparable, V any]( load func(context.Context, A, B, C) (V, error), options ...Option, ) Func3[A, B, C, V]
Func3 creates an operation keyed by its arguments.
func (*Memoizer) Func3By ¶
func (m *Memoizer) Func3By[A, B, C any, K comparable, V any]( load func(context.Context, A, B, C) (V, error), key func(A, B, C) K, options ...Option, ) Func3[A, B, C, V]
Func3By creates an operation keyed by key.
func (*Memoizer) Func4 ¶
func (m *Memoizer) Func4[A, B, C, D comparable, V any]( load func(context.Context, A, B, C, D) (V, error), options ...Option, ) Func4[A, B, C, D, V]
Func4 creates an operation keyed by its arguments.
func (*Memoizer) Func4By ¶
func (m *Memoizer) Func4By[A, B, C, D any, K comparable, V any]( load func(context.Context, A, B, C, D) (V, error), key func(A, B, C, D) K, options ...Option, ) Func4[A, B, C, D, V]
Func4By creates an operation keyed by key.
type Once ¶
type Once[V any] struct { // contains filtered or unexported fields }
Once is a memoized operation with no ordinary arguments.
func (Once[V]) Clear ¶
func (o Once[V]) Clear()
Clear invalidates every result and in-progress generation.
type Option ¶
type Option func(*operationOptions)
Option configures one operation.
func CacheErrorsFor ¶
CacheErrorsFor caches errors accepted by selected for duration.
A zero duration disables negative caching and permits a nil selector.
Context cancellation, dependency-cycle, panic, and Goexit errors are always excluded, regardless of selected.
func Named ¶
Named overrides the operation name inferred from its loader. An empty name uses the inferred name.
func StaleIfError ¶
StaleIfError retains successful values for duration after their fresh TTL. A zero duration disables stale fallback.
type PanicError ¶
PanicError reports a panic recovered from a loader.
type Policy ¶
type Policy struct {
// TTL is the fixed lifetime of a successful value, measured from loader
// completion. Cache hits do not extend it.
TTL time.Duration
// Capacity is the maximum number of completed values retained. In-flight
// loads do not count toward this limit.
Capacity int
// Jitter randomly subtracts up to this duration from TTL.
Jitter time.Duration
}
Policy controls the default policy of each operation in a Memoizer.
TTL and Capacity must both be positive.
type Stats ¶
type Stats struct {
// Hits counts Calls served by a fresh completed result.
Hits uint64
// Misses counts Calls whose initial lookup found no fresh result.
Misses uint64
// Loads counts physical loader invocations.
Loads uint64
// Coalesced counts calls attached to an existing in-flight load.
Coalesced uint64
// LoadErrors counts physical loads ending in an error, panic, or Goexit.
LoadErrors uint64
// StaleHits counts calls that fell back to a stale success.
StaleHits uint64
// ErrorHits counts calls served by a negatively cached result.
ErrorHits uint64
// Evictions counts completed results removed to enforce Capacity.
Evictions uint64
// Entries is the number of completed results currently retained. Because
// expiration is lazy, it may include expired entries that have not been
// accessed or displaced.
Entries int
// InFlight is the number of loader goroutines that have not returned. It
// includes detached loads and may exceed one per key.
InFlight int
}
Stats is a point-in-time snapshot of cache activity.
Counters are cumulative for the cache's lifetime; Clear does not reset them. Entries and InFlight are instantaneous gauges.