cache

package
v0.0.0-...-b17460c Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package cache implements a typed cache which can serve stale data while fetching fresh data, and which takes a distributed lock before attempting a refresh in order to greatly reduce possible cache stampede effects.

There is minimal support for multiple backends via NewCacheMultipleBackends. This is intended to be used for short durations to support migrating from one backend to another. It acquires pessimistic locks for write operations, so performance will be worse.

Data is stored in Redis, via the supplied Redis client.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrDoesNotExist is returned if negative caching is enabled and the
	// non-existence of the specified key has been cached. It must also be
	// returned by cache fetchers when the specified key does not exist and
	// negative caching is wanted.
	ErrDoesNotExist = errors.New("requested item does not exist")

	// ErrDisallowedCacheValue is thrown if a client attempts to set an entry in
	// the cache to the zero value of the cache type T. This is disallowed to
	// prevent accidentally poisoning the cache with invalid data.
	ErrDisallowedCacheValue = errors.New("nil and zero values are not permitted")
)

Functions

This section is empty.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[T any](
	client redis.Cmdable,
	name string,
	fresh, stale time.Duration,
	options ...Option,
) *Cache[T]

func NewCacheMultipleBackends

func NewCacheMultipleBackends[T any](
	clients []redis.Cmdable,
	name string,
	fresh, stale time.Duration,
	options ...Option,
) *Cache[T]

func (*Cache[T]) Get

func (c *Cache[T]) Get(ctx context.Context, key string, fetcher Fetcher[T]) (value T, err error)

Get fetches an item with the given key from cache. In the event of a cache miss or an error communicating with the cache, it will fall back to fetching the item from source using the passed fetcher.

func (*Cache[T]) Prepare

func (c *Cache[T]) Prepare(ctx context.Context) error

func (*Cache[T]) Set

func (c *Cache[T]) Set(ctx context.Context, key string, value T) error

Set updates the value stored in a given key with a provided object. This is not always needed (as usually values are fetched using the provided Fetcher[T]) but can be useful in some cases.

type Fetcher

type Fetcher[T any] func(ctx context.Context, key string) (T, error)

type Option

type Option interface {
	// contains filtered or unexported methods
}

func WithNegativeCaching

func WithNegativeCaching(duration time.Duration) Option

WithNegativeCaching configures the cache to allow caching of a negative ("does not exist") result for up to the specified duration.

func WithShadowWriteClient

func WithShadowWriteClient(client redis.Cmdable, timeout time.Duration) Option

WithShadowWrite configures the cache to use a separate Redis client for shadow writes. This is useful for writing an additional copy of data to a different Redis instance than the primary instance used for caching.

Jump to

Keyboard shortcuts

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