cache

package
v0.1.112 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package cache is the official cache pack: string keys with TTL on Valkey (or Redis), Remember for the read-through pattern and Incr for counters every node shares (a rate limit, a quota). CACHE_URL=memory gives a bounded in-process cache for tests and single-node development; production uses the server so every node sees the same entries.

Index

Constants

This section is empty.

Variables

View Source
var ErrMiss = errors.New("cache miss")

ErrMiss is returned by Get when the key is absent or expired.

View Source
var ErrNotCounter = errors.New("cache: the store does not implement Counter")

ErrNotCounter is returned by Incr when the store cannot count.

Functions

func Pack

func Pack() lidza.Pack

Pack returns the pack for packs.go.

func Remember

func Remember[T any](ctx context.Context, c *Cache, key string, ttl time.Duration, load func(ctx context.Context) (T, error)) (T, error)

Remember returns the cached value for key or computes it with load, stores it for ttl and returns it. Errors from load are not cached.

Types

type Cache

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

Cache is the running pack.

func From

func From(ctx context.Context) *Cache

From returns the cache from a request context.

func New

func New(store Store, prefix string) *Cache

New builds a cache over a store outside the pack lifecycle.

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, keys ...string) error

Delete removes keys.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string, out any) error

Get decodes the JSON stored under key into out; ErrMiss when absent.

func (*Cache) Incr

func (c *Cache) Incr(ctx context.Context, key string, by int64, ttl time.Duration) (int64, error)

Incr adds by (negative to subtract) to the counter under key and returns the new value, atomically across every node sharing the server. The increment that creates the key sets its ttl, so a counter per window resets on its own:

n, err := cache.From(ctx).Incr(ctx, "rate:"+clientIP+":"+minute, 1, time.Minute)
if n > 100 { ... refuse ... }

Later increments keep that expiry (a fixed window, not a sliding one). Get reads the counter into an int64. A key holding a non-integer value is an error.

func (*Cache) Invalidate

func (c *Cache) Invalidate(ctx context.Context, prefix string) error

Invalidate removes every key under prefix.

func (*Cache) Name

func (c *Cache) Name() string

Name implements lidza.Pack.

func (*Cache) Ready

func (c *Cache) Ready(ctx context.Context) error

Ready implements telemetry.Ready.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, value any, ttl time.Duration) error

Set stores value as JSON for ttl (0 keeps it until deleted).

func (*Cache) Start

func (c *Cache) Start(ctx context.Context, s *lidza.Services) error

Start reads the configuration and connects.

func (*Cache) Stop

func (c *Cache) Stop(context.Context) error

Stop closes the backend.

type Config

type Config struct {
	// URL is redis://host:port, valkey://..., or "memory".
	URL string `env:"CACHE_URL" required:"true"`
	// Prefix namespaces keys, so several apps can share a server.
	Prefix string `env:"CACHE_PREFIX" default:"lidza:"`
	// MemoryMax bounds the in-process backend's entries.
	MemoryMax int `env:"CACHE_MEMORY_MAX" default:"10000"`
}

Config comes from the environment.

type Counter

type Counter interface {
	// Incr adds by to the integer under key atomically and returns the
	// new value. A missing key starts at 0 and gets ttl (0: none); an
	// existing key keeps its expiry.
	Incr(ctx context.Context, key string, by int64, ttl time.Duration) (int64, error)
}

Counter is the backend contract of Incr, apart from Store so a backend written for an older version still satisfies Store. Memory and Valkey implement it.

type Memory

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

Memory is the bounded in-process backend.

func NewMemory

func NewMemory(max int) *Memory

NewMemory returns a backend holding at most max entries.

func (*Memory) Close

func (m *Memory) Close() error

func (*Memory) Delete

func (m *Memory) Delete(_ context.Context, keys ...string) error

func (*Memory) DeletePrefix

func (m *Memory) DeletePrefix(_ context.Context, prefix string) error

func (*Memory) Get

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

func (*Memory) Incr

func (m *Memory) Incr(_ context.Context, key string, by int64, ttl time.Duration) (int64, error)

Incr implements Counter under the store's lock.

func (*Memory) Len

func (m *Memory) Len() int

Len counts live entries (tests).

func (*Memory) Ping

func (m *Memory) Ping(context.Context) error

func (*Memory) Set

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

type Store

type Store interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	Delete(ctx context.Context, keys ...string) error
	// DeletePrefix removes every key under prefix (an invalidation
	// sweep); the server backend scans, so keep prefixes specific.
	DeletePrefix(ctx context.Context, prefix string) error
	Ping(ctx context.Context) error
	Close() error
}

Store is the backend contract.

type Valkey

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

Valkey is the server backend.

func NewValkey

func NewValkey(ctx context.Context, url string) (*Valkey, error)

NewValkey connects to url.

func (*Valkey) Close

func (v *Valkey) Close() error

func (*Valkey) Delete

func (v *Valkey) Delete(ctx context.Context, keys ...string) error

func (*Valkey) DeletePrefix

func (v *Valkey) DeletePrefix(ctx context.Context, prefix string) error

func (*Valkey) Get

func (v *Valkey) Get(ctx context.Context, key string) ([]byte, error)

func (*Valkey) Incr

func (v *Valkey) Incr(ctx context.Context, key string, by int64, ttl time.Duration) (int64, error)

Incr implements Counter with INCRBY and PEXPIRE in one Lua script.

func (*Valkey) Ping

func (v *Valkey) Ping(ctx context.Context) error

func (*Valkey) Set

func (v *Valkey) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

Jump to

Keyboard shortcuts

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