cachepolicy

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 5 Imported by: 2

Documentation

Overview

Package cachepolicy provides a CachePolicy policy.

Index

Constants

View Source
const CacheKey key = 0

CacheKey is a key to use with a Context that stores the cache key.

Variables

This section is empty.

Functions

func ContextWithCacheKey added in v0.7.0

func ContextWithCacheKey(ctx context.Context, key string) context.Context

ContextWithCacheKey returns a context with the cache key.

Types

type Builder

type Builder[R any] interface {
	// WithKey builds caches that store successful execution results in a cache with the key. This key can be overridden by
	// providing a CacheKey in a Context used with an execution.
	WithKey(key string) Builder[R]

	// CacheIf specifies that a value result should only be cached if it satisfies the predicate. By default, any non-error
	// results will be cached.
	CacheIf(predicate func(R, error) bool) Builder[R]

	// OnCacheHit registers the listener to be called when the cachePolicy entry is hit during an execution.
	OnCacheHit(listener func(event failsafe.ExecutionDoneEvent[R])) Builder[R]

	// OnCacheMiss registers the listener to be called when the cachePolicy entry is missed during an execution.
	OnCacheMiss(listener func(event failsafe.ExecutionEvent[R])) Builder[R]

	// OnResultCached registers the listener to be called when a result is cached.
	OnResultCached(listener func(event failsafe.ExecutionEvent[R])) Builder[R]

	// Build returns a new CachePolicy using the builder's configuration.
	Build() CachePolicy[R]
}

Builder builds CachePolicy instances. In order for the cache policy to be used, a key must be provided via WithKey, or via a Context when the execution is performed using a value stored under the CacheKey in the Context. A cache key stored in a Context takes precedence over a cache key configured via WithKey.

R is the execution result type. This type is not concurrency safe.

func NewBuilder added in v0.7.0

func NewBuilder[R any](cache Cache[R]) Builder[R]

NewBuilder returns a Builder.

type Cache

type Cache[R any] interface {
	// Get gets and returns a cache entry along with a flag indicating if it's present.
	Get(key string) (R, bool)

	// Set stores a value for the key in the cache.
	Set(key string, value R)
}

Cache is a simple interface for cached values that can be adapted to different cache backends.

R is the execution result type.

type CachePolicy

type CachePolicy[R any] interface {
	failsafe.Policy[R]
}

CachePolicy is a read through cache Policy that sets and gets cached results for some key. The cache key can be configured via Builder, or by setting a CacheKey value in a Context used with an execution.

R is the execution result type. This type is concurrency safe.

func New added in v0.7.0

func New[R any](cache Cache[R]) CachePolicy[R]

New returns a new CachePolicy. The resulting CachePolicy will only be used with executions that provide a Context containing a CacheKey value.

Jump to

Keyboard shortcuts

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