kvs

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package kvs provides a unified key-value store abstraction with implementations for Memory, LevelDB, and Redis.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound is returned when a key is not found or has expired.
	ErrNotFound = errors.New("kvs: key not found")

	// ErrClosed is returned when an operation is attempted on a closed store.
	ErrClosed = errors.New("kvs: store is closed")
)

Common errors

Functions

This section is empty.

Types

type Config

type Config struct {
	// Type specifies the store type: "memory", "leveldb", or "redis"
	Type string `yaml:"type"`

	// Namespace provides logical isolation within the store.
	// - Memory: uses hierarchical map structure
	// - LevelDB: creates separate directory per namespace
	// - Redis: uses as key prefix
	Namespace string `yaml:"namespace"`

	// Memory-specific config
	Memory MemoryConfig `yaml:"memory"`

	// LevelDB-specific config
	LevelDB LevelDBConfig `yaml:"leveldb"`

	// Redis-specific config
	Redis RedisConfig `yaml:"redis"`
}

Config represents the configuration for creating a KVS store.

type LevelDBConfig

type LevelDBConfig struct {
	// Path is the directory path for LevelDB storage.
	// If empty, a temporary directory will be used (OS-dependent).
	Path string `yaml:"path"`

	// SyncWrites enables synchronous writes (slower but safer).
	SyncWrites bool `yaml:"sync_writes"`

	// CleanupInterval is how often to scan for and remove expired keys.
	// Default: 5 minutes
	CleanupInterval time.Duration `yaml:"cleanup_interval"`
}

LevelDBConfig configures the LevelDB store.

type LevelDBStore

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

LevelDBStore is a LevelDB-based implementation of Store. It provides persistent storage on the filesystem with background cleanup of expired keys. Each namespace gets its own LevelDB directory for complete isolation.

func NewLevelDBStore

func NewLevelDBStore(namespace string, cfg LevelDBConfig) (*LevelDBStore, error)

NewLevelDBStore creates a new LevelDB KVS store for the given namespace. Each namespace gets its own isolated LevelDB database directory.

func (*LevelDBStore) Close

func (l *LevelDBStore) Close() error

Close closes the LevelDB database and stops the cleanup goroutine.

func (*LevelDBStore) Count

func (l *LevelDBStore) Count(ctx context.Context, prefix string) (int, error)

Count returns the number of keys matching a prefix.

func (*LevelDBStore) Delete

func (l *LevelDBStore) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*LevelDBStore) Exists

func (l *LevelDBStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists and has not expired.

func (*LevelDBStore) Get

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

Get retrieves a value by key.

func (*LevelDBStore) List

func (l *LevelDBStore) List(ctx context.Context, keyPrefix string) ([]string, error)

List returns all keys matching a prefix.

func (*LevelDBStore) Set

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

Set stores a value with optional TTL.

type MemoryConfig

type MemoryConfig struct {
	// CleanupInterval is how often to scan for and remove expired keys.
	// Default: 5 minutes
	CleanupInterval time.Duration `yaml:"cleanup_interval"`
}

MemoryConfig configures the in-memory store.

type MemoryStore

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

MemoryStore is an in-memory implementation of Store. It stores data in a map and runs a background goroutine to clean up expired items. Data is volatile and will be lost when the process restarts. Each MemoryStore instance is isolated by namespace.

func NewMemoryStore

func NewMemoryStore(namespace string, cfg MemoryConfig) (*MemoryStore, error)

NewMemoryStore creates a new in-memory KVS store for the given namespace. Each namespace gets its own isolated MemoryStore instance.

func (*MemoryStore) Close

func (m *MemoryStore) Close() error

Close closes the store and stops the cleanup goroutine.

func (*MemoryStore) Count

func (m *MemoryStore) Count(ctx context.Context, prefix string) (int, error)

Count returns the number of keys matching a prefix.

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*MemoryStore) Exists

func (m *MemoryStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists and has not expired.

func (*MemoryStore) Get

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

Get retrieves a value by key.

func (*MemoryStore) List

func (m *MemoryStore) List(ctx context.Context, keyPrefix string) ([]string, error)

List returns all keys matching a prefix.

func (*MemoryStore) Set

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

Set stores a value with optional TTL.

type RedisConfig

type RedisConfig struct {
	// Addr is the Redis server address (host:port)
	Addr string `yaml:"addr"`

	// Password is the Redis password (optional)
	Password string `yaml:"password"`

	// DB is the Redis database number (0-15)
	DB int `yaml:"db"`

	// PoolSize is the maximum number of socket connections (0 = default 10 * runtime.NumCPU)
	PoolSize int `yaml:"pool_size"`
}

RedisConfig configures the Redis store.

type RedisStore

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

RedisStore is a Redis-based implementation of Store. It provides distributed, persistent storage backed by Redis. Namespace isolation is implemented using key prefixes (namespace:key format).

func NewRedisStore

func NewRedisStore(namespace string, cfg RedisConfig) (*RedisStore, error)

NewRedisStore creates a new Redis KVS store for the given namespace. Namespace isolation is achieved using key prefixes.

func (*RedisStore) Close

func (r *RedisStore) Close() error

Close closes the Redis connection.

func (*RedisStore) Count

func (r *RedisStore) Count(ctx context.Context, prefix string) (int, error)

Count returns the number of keys matching a prefix.

func (*RedisStore) Delete

func (r *RedisStore) Delete(ctx context.Context, key string) error

Delete removes a key.

func (*RedisStore) Exists

func (r *RedisStore) Exists(ctx context.Context, key string) (bool, error)

Exists checks if a key exists and has not expired.

func (*RedisStore) Get

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

Get retrieves a value by key.

func (*RedisStore) List

func (r *RedisStore) List(ctx context.Context, keyPrefix string) ([]string, error)

List returns all keys matching a prefix.

func (*RedisStore) Set

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

Set stores a value with optional TTL.

type Store

type Store interface {
	// Get retrieves a value by key.
	// Returns ErrNotFound if the key does not exist or has expired.
	Get(ctx context.Context, key string) ([]byte, error)

	// Set stores a value with optional TTL.
	// If ttl is 0, the key will not expire.
	// If ttl is negative, the behavior is implementation-defined (typically treated as 0).
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

	// Delete removes a key.
	// Does not return an error if the key does not exist.
	Delete(ctx context.Context, key string) error

	// Exists checks if a key exists and has not expired.
	Exists(ctx context.Context, key string) (bool, error)

	// List returns all keys matching a prefix.
	// Empty prefix returns all keys in the store.
	// The order of keys is not guaranteed.
	List(ctx context.Context, prefix string) ([]string, error)

	// Count returns the number of keys matching a prefix.
	// Empty prefix returns the total count of all keys.
	Count(ctx context.Context, prefix string) (int, error)

	// Close closes the store and releases resources.
	// After Close is called, all operations should return ErrClosed.
	Close() error
}

Store is a key-value store interface that supports TTL and basic operations. All implementations must be thread-safe.

func New

func New(cfg Config) (Store, error)

New creates a new KVS store based on the provided proxyserver. The Namespace field provides logical isolation - implementation varies by backend: - Memory: separate store instance per namespace - LevelDB: separate directory per namespace - Redis: key prefix per namespace

Jump to

Keyboard shortcuts

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