cache

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Label  string `json:"label" yaml:"label"`
	Type   string `json:"type" yaml:"type"`
	Plugin any    `json:"plugin,omitempty" yaml:"plugin,omitempty"`
}

Config is the all encompassing configuration struct for all cache types. Deprecated: Do not add new components here. Instead, use the public plugin APIs. Examples can be found in: ./internal/impl.

func FromAny

func FromAny(prov docs.Provider, value any) (conf Config, err error)

func NewConfig

func NewConfig() Config

NewConfig returns a configuration struct fully populated with default values.

type KeyIterator added in v1.21.0

type KeyIterator = iter.Seq2[string, error]

KeyIterator is an iterator over the keys held by a cache. Iteration stops after the first non-nil error.

func PrefetchKeys added in v1.21.0

func PrefetchKeys(ctx context.Context, readAhead int, produce func(ctx context.Context, emit func(key string) bool) error) KeyIterator

PrefetchKeys builds a Keys iterator that runs the provided produce function in a background goroutine, allowing it to fetch subsequent pages of keys while the caller consumes the keys already found. Up to readAhead keys are buffered; sizing this at or above a backend's page size lets the next page be fetched while the current one is being yielded.

The produce function should call emit once for each key. emit returns false once the caller has stopped consuming (an early break, or a downstream error), after which produce should stop and return - typically nil. Returning a non-nil error from produce terminates iteration after yielding that error to the caller. If ctx is cancelled iteration stops without yielding a further error; produce should return promptly when ctx is done.

This is a helper for implementing the optional key-listing behaviour of a paginated cache; it manages the goroutine lifecycle and cancellation on early termination so implementations only need to express their paging loop. It is intended for use by bento's own cache components.

type Listable added in v1.21.0

type Listable interface {
	V1

	// Keys returns an iterator over all keys currently held by the cache.
	// Iteration stops after the first non-nil error, caches that are unable
	// to enumerate their keys yield component.ErrKeyListingNotSupported.
	Keys(ctx context.Context) KeyIterator
}

func MetricsForListableCache added in v1.21.0

func MetricsForListableCache(c Listable, stats metrics.Type) Listable

type TTLItem

type TTLItem struct {
	Value []byte
	TTL   *time.Duration
}

TTLItem contains a value to cache along with an optional TTL.

type V1

type V1 interface {
	// Get attempts to locate and return a cached value by its key, returns an
	// error if the key does not exist or if the command fails.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set attempts to set the value of a key, returns an error if the command
	// fails.
	Set(ctx context.Context, key string, value []byte, ttl *time.Duration) error

	// SetMulti attempts to set the value of multiple keys, returns an error if
	// any of the keys fail.
	SetMulti(ctx context.Context, items map[string]TTLItem) error

	// Add attempts to set the value of a key only if the key does not already
	// exist, returns an error if the key already exists or if the command
	// fails.
	Add(ctx context.Context, key string, value []byte, ttl *time.Duration) error

	// Exists attempts to locate a cached value by its key, returns
	// false if the key does not exist, returns an if the command fails.
	Exists(ctx context.Context, key string) (bool, error)

	// Delete attempts to remove a key. Returns an error if a failure occurs.
	Delete(ctx context.Context, key string) error

	// Close the component, blocks until either the underlying resources are
	// cleaned up or the context is cancelled. Returns an error if the context
	// is cancelled.
	Close(ctx context.Context) error
}

V1 Defines a common interface of cache implementations.

func MetricsForCache

func MetricsForCache(c V1, stats metrics.Type) V1

MetricsForCache wraps a cache with a struct that adds standard metrics over each method.

Jump to

Keyboard shortcuts

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