cache

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides a small cache contract with in-memory and Redis drivers behind one small interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Remember

func Remember[T any](ctx context.Context, store Store, key string, ttl time.Duration, compute func(context.Context) (T, error)) (T, error)

Remember returns the cached JSON-decoded value for key, computing and storing it on a miss. A stored value that no longer decodes into T is treated as a miss and recomputed, so shape changes self-heal.

Types

type Memory

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

Memory is an in-process Store suitable for development and single-instance deployments. It is unbounded; use the Redis driver when eviction or shared state is needed.

func NewMemory

func NewMemory(options MemoryOptions) *Memory

NewMemory performs this package operation.

func (*Memory) Close

func (m *Memory) Close() error

Close stops the janitor. It is idempotent.

func (*Memory) Forget

func (m *Memory) Forget(_ context.Context, key string) error

Forget performs this package operation.

func (*Memory) Get

func (m *Memory) Get(_ context.Context, key string) (string, bool, error)

Get performs this package operation.

func (*Memory) Increment

func (m *Memory) Increment(_ context.Context, key string, by int64) (int64, error)

Increment performs this package operation.

func (*Memory) Set

func (m *Memory) Set(_ context.Context, key, value string, ttl time.Duration) error

Set performs this package operation.

type MemoryOptions

type MemoryOptions struct {
	// CleanupInterval between janitor sweeps of expired entries; defaults to
	// one minute. A negative interval disables the janitor.
	CleanupInterval time.Duration
}

MemoryOptions defines an implementation type used by this package.

type Redis

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

Redis is a Store backed by a shared Redis client.

func NewRedis

func NewRedis(options RedisOptions) (*Redis, error)

NewRedis performs this package operation.

func (*Redis) Close

func (r *Redis) Close() error

Close is a no-op: the underlying client is owned by the application.

func (*Redis) Forget

func (r *Redis) Forget(ctx context.Context, key string) error

Forget performs this package operation.

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string) (string, bool, error)

Get performs this package operation.

func (*Redis) Increment

func (r *Redis) Increment(ctx context.Context, key string, by int64) (int64, error)

Increment performs this package operation.

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, key, value string, ttl time.Duration) error

Set performs this package operation.

type RedisOptions

type RedisOptions struct {
	// Client is required. The caller owns it and closes it; the runtime
	// shares one client between the cache and other Redis consumers.
	Client *redis.Client
	// Prefix is prepended to every key, e.g. "myapp:".
	Prefix string
}

RedisOptions defines an implementation type used by this package.

type Store

type Store interface {
	// Get returns the value for key; ok reports whether the key existed.
	Get(ctx context.Context, key string) (value string, ok bool, err error)
	// Set stores value under key. A ttl of zero or less stores it without
	// expiry.
	Set(ctx context.Context, key, value string, ttl time.Duration) error
	// Forget removes key; deleting a missing key is not an error.
	Forget(ctx context.Context, key string) error
	// Increment adds by to the integer stored under key, starting from zero
	// when the key is missing, and returns the new value. A non-integer value
	// is an error.
	Increment(ctx context.Context, key string, by int64) (int64, error)
	// Close define an operation required by this interface.
	Close() error
}

Store is the cache contract shared by every driver.

Jump to

Keyboard shortcuts

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