cachepolicy

package
v0.6.9 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 5 Imported by: 0

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

This section is empty.

Types

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.

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 CachePolicyBuilder, or by setting a CacheKey value in a Context used with an execution.

This type is concurrency safe.

func With

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

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

type CachePolicyBuilder

type CachePolicyBuilder[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) CachePolicyBuilder[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) CachePolicyBuilder[R]

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

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

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

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

CachePolicyBuilder 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.

This type is not concurrency safe.

func Builder

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

Builder returns a CachePolicyBuilder.

Jump to

Keyboard shortcuts

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