cache

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotFound indicates no value exists for the given key.
	ErrKeyNotFound = errors.New("key not found")
	// ErrKeyExpired indicates a value exists but has expired.
	ErrKeyExpired = errors.New("key expired")
	// ErrKeyExists indicates a conflicting set when the key already exists.
	ErrKeyExists = errors.New("key already exists")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set sets the value for the given key in the cache.
	Set(ctx context.Context, key string, value string, opts ...Option) error

	// SetOrFail is like Set, but returns ErrKeyExists if the key already exists.
	SetOrFail(ctx context.Context, key string, value string, opts ...Option) error

	// Get gets the value for the given key from the cache.
	//
	// If the key is not found, it returns ErrKeyNotFound.
	// If the key has expired, it returns ErrKeyExpired.
	// Otherwise, it returns the value and nil.
	Get(ctx context.Context, key string) (string, error)

	// GetAndDelete is like Get, but also deletes the key from the cache.
	GetAndDelete(ctx context.Context, key string) (string, error)

	// Delete removes the item associated with the given key from the cache.
	// If the key does not exist, it performs no action and returns nil.
	// The operation is safe for concurrent use.
	Delete(ctx context.Context, key string) error

	// Cleanup removes all expired items from the cache.
	// The operation is safe for concurrent use.
	Cleanup(ctx context.Context) error

	// Drain returns a map of all the non-expired items in the cache.
	// The returned map is a snapshot of the cache at the time of the call.
	// The cache is cleared after the call.
	// The operation is safe for concurrent use.
	Drain(ctx context.Context) (map[string]string, error)
}

func NewMemory

func NewMemory(ttl time.Duration) Cache

func NewRedis

func NewRedis(client *redis.Client, prefix string, ttl time.Duration) Cache

type Option

type Option func(*options)

Option configures per-item cache behavior (e.g., expiry).

func WithTTL

func WithTTL(ttl time.Duration) Option

WithTTL is an Option that sets the TTL (time to live) for an item, i.e. the item will expire after the given duration from the time of insertion.

func WithValidUntil

func WithValidUntil(validUntil time.Time) Option

WithValidUntil is an Option that sets the valid until time for an item, i.e. the item will expire at the given time.

Jump to

Keyboard shortcuts

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