cache

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 11 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cache provides Gombit's backend-neutral cache boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecrementExisting added in v0.9.0

func DecrementExisting(ctx context.Context, c Cache, key string, delta int64) (int64, bool, error)

DecrementExisting takes delta off key in c only if it exists, through c's Counter when it has one. For a Cache without one it decrements, then deletes the key when the decrement created it (the result is -delta: no counter was there): not atomic, so between the two another caller's increment of the new key can be lost with it (an undercount, never a negative counter left behind), and a counter that held exactly 0 is deleted rather than left at -delta (the same count, as far as a limit goes). found is false for a key that did not exist.

func IncrementTTL added in v0.9.0

func IncrementTTL(ctx context.Context, c Cache, key string, delta int64, ttl time.Duration) (int64, error)

IncrementTTL increments key in c by delta and gives it ttl when it has no expiry, through c's Counter when it has one. For a Cache without one it increments, then sets the counter again with ttl when the increment created it: not atomic, so an increment by another caller between the two can be lost (an undercount at the start of a window, never a counter that lives forever). ttl must be positive.

func NewRedisClient

func NewRedisClient(cfg config.RedisConfig) (*redis.Client, error)

NewRedisClient builds a go-redis client from typed configuration.

func RedisOptions added in v0.5.0

func RedisOptions(cfg config.RedisConfig) *redis.Options

RedisOptions maps the typed Redis settings to go-redis options, for a caller that tunes them further before building a client (the job queue).

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string, dst any) (bool, error)
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Delete(ctx context.Context, keys ...string) error
	Increment(ctx context.Context, key string, delta int64) (int64, error)
}

Cache is the value-oriented cache contract used by framework features.

func Namespace

func Namespace(namespace string, next Cache) Cache

Namespace prefixes all keys before forwarding operations to next.

type Counter added in v0.9.0

type Counter interface {
	IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
	DecrementExisting(ctx context.Context, key string, delta int64) (value int64, found bool, err error)
}

Counter is implemented by caches whose counters can expire: rate limits, attempt counters. IncrementTTL increments key by delta, as Increment does, and gives the key ttl when it has no expiry (it was just created, or written without one); a key that already expires keeps its deadline, so a fixed window is not stretched by every hit. Atomic: concurrent calls never lose an increment or leave a counter without its expiry. ttl must be positive: a counter that never expires is what Increment is for.

DecrementExisting takes delta off key only if the key exists (and has not expired), keeping its expiry: it never creates a counter, so giving back a place in a counter that was cleared or evicted meanwhile writes nothing (a create-on-miss decrement would leave a negative counter). found reports whether the key existed.

The built-in drivers (memory, redis, noop) and Namespace implement it; IncrementTTL and DecrementExisting (the functions) fall back, not atomically, for a Cache that does not.

type Driver

type Driver string

Driver names an opened cache driver.

const (
	// DriverMemory stores cache values in process memory.
	DriverMemory Driver = Driver(config.CacheDriverMemory)
	// DriverRedis stores cache values in Redis.
	DriverRedis Driver = Driver(config.CacheDriverRedis)
	// DriverNoop disables persistence while preserving cache call sites.
	DriverNoop Driver = Driver(config.CacheDriverNoop)
)

type Memory

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

Memory stores cache values in process memory.

func NewMemory

func NewMemory(opts ...MemoryOption) *Memory

NewMemory creates an empty in-process cache.

func (*Memory) Close added in v0.1.10

func (m *Memory) Close() error

Close stops the janitor goroutine started by WithJanitor, if any. Safe to call on a Memory with no janitor, and safe to call more than once.

func (*Memory) DecrementExisting added in v0.9.0

func (m *Memory) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)

DecrementExisting implements Counter.

func (*Memory) Delete

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

Delete implements Cache.

func (*Memory) Get

func (m *Memory) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (*Memory) Increment

func (m *Memory) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (*Memory) IncrementTTL added in v0.9.0

func (m *Memory) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

IncrementTTL implements Counter.

func (*Memory) Set

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

Set implements Cache.

type MemoryOption added in v0.1.10

type MemoryOption func(*Memory)

MemoryOption configures a Memory cache created by NewMemory.

func WithJanitor added in v0.1.10

func WithJanitor(interval time.Duration) MemoryOption

WithJanitor starts a background goroutine that sweeps expired entries every interval, stopped by Close. Without this option, Memory never runs a goroutine and only reclaims an expired key when that same key is read again via Get — the caller must opt in for proactive reclamation of write-heavy, rarely-re-read keys (rate limits, one-time tokens, nonces).

Close must be called to stop the goroutine. cache.Open wires this automatically for the memory driver, and framework.App closes it on shutdown for a cache the App opened itself. If you construct a Memory with WithJanitor yourself and hand it to framework.WithCache (or keep it outside framework.App entirely), framework.App does not close a cache it did not open — you are responsible for calling Close, or the goroutine (and the Memory it holds) leaks for the life of the process.

A non-positive interval means no janitor: no goroutine is started, as if the option were omitted (issue #436). time.NewTicker panics on such a value, and it would do so inside the janitor goroutine, where the caller cannot recover it, so an unset or misparsed interval used to crash the whole process.

type Noop

type Noop struct{}

Noop disables persistence while preserving cache call sites.

func (Noop) DecrementExisting added in v0.9.0

func (Noop) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)

DecrementExisting implements Counter: nothing exists.

func (Noop) Delete

func (Noop) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (Noop) Get

func (Noop) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (Noop) Increment

func (Noop) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (Noop) IncrementTTL added in v0.9.0

func (Noop) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

IncrementTTL implements Counter: it keeps nothing, as Increment.

func (Noop) Set

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

Set implements Cache.

type Redis

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

Redis stores cache values in Redis.

func NewRedis

func NewRedis(client *redis.Client) *Redis

NewRedis wraps a go-redis client with Gombit's cache contract.

func (*Redis) Client

func (r *Redis) Client() *redis.Client

Client returns the underlying go-redis client.

func (*Redis) DecrementExisting added in v0.9.0

func (r *Redis) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)

DecrementExisting implements Counter.

func (*Redis) Delete

func (r *Redis) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (*Redis) Get

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

Get implements Cache.

func (*Redis) Increment

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

Increment implements Cache.

func (*Redis) IncrementTTL added in v0.9.0

func (r *Redis) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

IncrementTTL implements Counter.

func (*Redis) Set

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

Set implements Cache.

type Store

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

Store is an opened cache with driver metadata and optional Redis escape hatch.

func Open

func Open(cfg config.CacheConfig) (*Store, error)

Open opens the configured cache driver.

func (*Store) Close

func (s *Store) Close() error

Close closes driver-owned resources.

func (*Store) DecrementExisting added in v0.9.0

func (s *Store) DecrementExisting(ctx context.Context, key string, delta int64) (int64, bool, error)

DecrementExisting implements Counter.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, keys ...string) error

Delete implements Cache.

func (*Store) Driver

func (s *Store) Driver() Driver

Driver returns the configured driver.

func (*Store) Get

func (s *Store) Get(ctx context.Context, key string, dst any) (bool, error)

Get implements Cache.

func (*Store) Increment

func (s *Store) Increment(ctx context.Context, key string, delta int64) (int64, error)

Increment implements Cache.

func (*Store) IncrementTTL added in v0.9.0

func (s *Store) IncrementTTL(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

IncrementTTL implements Counter.

func (*Store) Redis

func (s *Store) Redis() *redis.Client

Redis returns the underlying go-redis client when the Redis driver is enabled.

func (*Store) Set

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

Set implements Cache.

Jump to

Keyboard shortcuts

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