Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Execute ¶
func Execute( ctx context.Context, executionKey interface{}, memoizedFn func(context.Context) (interface{}, error), ) (result interface{}, err error, isMemoized bool)
Execute guarantees that the given memoizedFn will be invoked only once regardless of how many times Execute gets called with the same executionKey. All callers will receive the same result and error as the result of this call.
Note 1: this promise can only be kept if the given context has been initialized using WithCache before calling Execute. The last return value indicates whether this function call was memoized or not.
Note 2: The provided key must be comparable and should not be of type string or any other built-in type to avoid collisions between packages using this context. Callers of Execute should define their own types for keys.
Types ¶
type DestroyFn ¶
type DestroyFn func()
func WithCache ¶
WithCache returns a new context.Context that holds a reference to a cache for memoized functions. This is meant to be a request-level cache that will automatically get garbage-collected at the end of an API request when the context itself is garbage-collected.
Note: the return DestroyFn must be deferred to minimize memory leaks.
type Function ¶
Function is the type of function that can be memoized.
The argument must not materially affect the result of the function in ways that are not captured by the promise's key, since if promise.get is called twice concurrently, with the same (implicit) key but different arguments, the Function is called only once but its result must be suitable for both callers.