cache

package
v1.34.4 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidConfig indicates an invalid configuration.
	ErrInvalidConfig = errors.New("invalid config")
	// 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 []byte, opts ...Option) error

	// SetOrFail is like Set, but returns ErrKeyExists if the key already exists.
	SetOrFail(ctx context.Context, key string, value []byte, 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, opts ...GetOption) ([]byte, error)

	// GetAndDelete is like Get, but also deletes the key from the cache.
	GetAndDelete(ctx context.Context, key string) ([]byte, 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][]byte, error)

	// Close closes the cache.
	// The operation is safe for concurrent use.
	Close() error
}

type GetOption added in v1.31.0

type GetOption func(*getOptions)

func AndDefaultTTL added in v1.31.0

func AndDefaultTTL() GetOption

func AndDelete added in v1.31.0

func AndDelete() GetOption

func AndSetTTL added in v1.31.0

func AndSetTTL(ttl time.Duration) GetOption

func AndSetValidUntil added in v1.31.0

func AndSetValidUntil(validUntil time.Time) GetOption

func AndUpdateTTL added in v1.31.0

func AndUpdateTTL(ttl time.Duration) GetOption

type MemoryCache added in v1.34.3

type MemoryCache struct {
	// contains filtered or unexported fields
}

func NewMemory

func NewMemory(ttl time.Duration) *MemoryCache

func (*MemoryCache) Cleanup added in v1.34.3

func (m *MemoryCache) Cleanup(_ context.Context) error

Cleanup implements Cache.

func (*MemoryCache) Close added in v1.34.3

func (m *MemoryCache) Close() error

func (*MemoryCache) Delete added in v1.34.3

func (m *MemoryCache) Delete(_ context.Context, key string) error

Delete implements Cache.

func (*MemoryCache) Drain added in v1.34.3

func (m *MemoryCache) Drain(_ context.Context) (map[string][]byte, error)

Drain implements Cache.

func (*MemoryCache) Get added in v1.34.3

func (m *MemoryCache) Get(_ context.Context, key string, opts ...GetOption) ([]byte, error)

Get implements Cache.

func (*MemoryCache) GetAndDelete added in v1.34.3

func (m *MemoryCache) GetAndDelete(ctx context.Context, key string) ([]byte, error)

GetAndDelete implements Cache.

func (*MemoryCache) Set added in v1.34.3

func (m *MemoryCache) Set(_ context.Context, key string, value []byte, opts ...Option) error

Set implements Cache.

func (*MemoryCache) SetOrFail added in v1.34.3

func (m *MemoryCache) SetOrFail(_ context.Context, key string, value []byte, opts ...Option) error

SetOrFail implements 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.

type RedisCache added in v1.34.3

type RedisCache struct {
	// contains filtered or unexported fields
}

func NewRedis

func NewRedis(config RedisConfig) (*RedisCache, error)

func (*RedisCache) Cleanup added in v1.34.3

func (r *RedisCache) Cleanup(_ context.Context) error

Cleanup implements Cache.

func (*RedisCache) Close added in v1.34.3

func (r *RedisCache) Close() error

func (*RedisCache) Delete added in v1.34.3

func (r *RedisCache) Delete(ctx context.Context, key string) error

Delete implements Cache.

func (*RedisCache) Drain added in v1.34.3

func (r *RedisCache) Drain(ctx context.Context) (map[string][]byte, error)

Drain implements Cache.

func (*RedisCache) Get added in v1.34.3

func (r *RedisCache) Get(ctx context.Context, key string, opts ...GetOption) ([]byte, error)

Get implements Cache.

func (*RedisCache) GetAndDelete added in v1.34.3

func (r *RedisCache) GetAndDelete(ctx context.Context, key string) ([]byte, error)

GetAndDelete implements Cache.

func (*RedisCache) Set added in v1.34.3

func (r *RedisCache) Set(ctx context.Context, key string, value []byte, opts ...Option) error

Set implements Cache.

func (*RedisCache) SetOrFail added in v1.34.3

func (r *RedisCache) SetOrFail(ctx context.Context, key string, value []byte, opts ...Option) error

SetOrFail implements Cache.

type RedisConfig added in v1.32.0

type RedisConfig struct {
	// Client is the Redis client to use.
	// If nil, a client is created from the URL.
	Client *redis.Client

	// URL is the Redis URL to use.
	// If empty, the Redis client is not created.
	URL string

	// Prefix is the prefix to use for all keys in the Redis cache.
	Prefix string

	// TTL is the time-to-live for all cache entries.
	TTL time.Duration
}

RedisConfig configures the Redis cache backend.

Jump to

Keyboard shortcuts

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