kv

package
v0.9.7 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("state: key not found")

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Key   string
	Value []byte
}

type Mem

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

Mem is an in-memory Store backed by sync.Map.

func NewMem

func NewMem() *Mem

NewMem constructs a Mem and starts the TTL janitor.

func (*Mem) Close

func (m *Mem) Close() error

func (*Mem) Del

func (m *Mem) Del(_ context.Context, key string) error

func (*Mem) Expire

func (m *Mem) Expire(_ context.Context, key string, ttl time.Duration) error

func (*Mem) Get

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

func (*Mem) HGet

func (m *Mem) HGet(_ context.Context, key, field string) ([]byte, error)

HGet reads one field of a hash stored as synthetic keys "key\x00field". Returns ErrNotFound when either the hash key or the field is absent.

func (*Mem) HSet

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

HSet writes one field of a hash stored as synthetic keys "key\x00field". When ttl > 0 the field entry gets that TTL; ttl == 0 preserves existing TTL.

func (*Mem) Incr

func (m *Mem) Incr(_ context.Context, key string, delta int64) (int64, error)

func (*Mem) Range

func (m *Mem) Range(_ context.Context, prefix string) ([]Entry, error)

func (*Mem) RegisterScript

func (m *Mem) RegisterScript(name string, fn ScriptImpl)

RegisterScript registers a Go emulator for a named script.

func (*Mem) RunScript

func (m *Mem) RunScript(ctx context.Context, name, _ string, keys []string, args ...any) ([]byte, error)

RunScript looks up the named emulator and invokes it.

func (*Mem) Set

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

func (*Mem) WithLock

func (m *Mem) WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error

type Redis

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

Redis implements Store (and Scripter) backed by Redis/Valkey.

func NewRedis

func NewRedis(ctx context.Context, cfg RedisConfig) (*Redis, error)

NewRedis constructs a Redis and pings the server. Precedence: ClusterAddrs > Sentinel > Addr (single-node).

func (*Redis) Close

func (r *Redis) Close() error

func (*Redis) Del

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

func (*Redis) Expire

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

func (*Redis) Get

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

func (*Redis) Incr

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

func (*Redis) Ping

func (r *Redis) Ping(ctx context.Context) error

Ping checks the connection.

func (*Redis) Range

func (r *Redis) Range(ctx context.Context, prefix string) ([]Entry, error)

TODO(kv): Cluster-unsafe — SCAN only covers one shard.

func (*Redis) RunScript

func (r *Redis) RunScript(ctx context.Context, name, script string, keys []string, args ...any) ([]byte, error)

RunScript implements Scripter.

func (*Redis) Set

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

func (*Redis) WithLock

func (r *Redis) WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error

WithLock implements the blocking Store contract over SET NX PX: acquisition is retried with jittered backoff (5-25ms) until it succeeds or ctx is done, matching Mem's block-until-acquired semantics. The all-or-nothing Lua acquire (partial holds are rolled back before returning 0) keeps opposite key orders deadlock-free while polling. Cluster safety: all keys must share the same hash tag, else CROSSSLOT.

type RedisConfig

type RedisConfig struct {
	Addr         string
	Sentinel     *SentinelConfig
	ClusterAddrs []string // non-empty → Cluster mode (redis.NewClusterClient)
	DB           int
	Password     string
	PoolSize     int
	MinIdleConns int
}

RedisConfig configures a Redis store. Exactly one of Addr, Sentinel, or ClusterAddrs should be set. When ClusterAddrs is non-empty, a redis.ClusterClient is used and all multi-key operations (RunScript, WithLock) require keys to share the same hash tag to avoid CROSSSLOT errors.

type ScriptImpl

type ScriptImpl func(ctx context.Context, store *Mem, keys []string, args []any) ([]byte, error)

ScriptImpl is the Go-emulator function registered on MemStore. It receives the store so it can call Get/Set/Incr etc. directly.

type Scripter

type Scripter interface {
	RunScript(ctx context.Context, name, script string, keys []string, args ...any) ([]byte, error)
}

ScriptRunner is an optional interface implemented by stores that support named Lua (or equivalent) scripts. Consumers type-assert to opt in.

type SentinelConfig

type SentinelConfig struct {
	MasterName       string
	SentinelAddrs    []string
	SentinelPassword string
}

SentinelConfig configures Sentinel-mode failover.

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
	// Del removes a key. It is a no-op (no error) when the key does not exist.
	Del(ctx context.Context, key string) error
	Incr(ctx context.Context, key string, delta int64) (int64, error)
	Expire(ctx context.Context, key string, ttl time.Duration) error
	Range(ctx context.Context, prefix string) ([]Entry, error)
	// WithLock runs fn while holding an advisory lock over all keys.
	// BLOCKING contract: under contention the call waits (retrying with
	// backoff on distributed backends) until the lock is acquired — every
	// contender eventually runs fn, or returns the ctx error if ctx is
	// cancelled/expires while waiting. It never returns a "busy" error
	// without running fn. Mutual exclusion holds across all keys for the
	// duration of fn.
	WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error
	Close() error
}

Store is the operational-state adapter. M1 ships MemStore; M5 will add a Redis-backed implementation.

Jump to

Keyboard shortcuts

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