kv

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: AGPL-3.0 Imports: 12 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

Index

Constants

This section is empty.

Variables

View Source
var ErrLockBusy = errors.New("state: lock busy")
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

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(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