Documentation
¶
Overview ¶
Package kvs provides a unified key-value store abstraction with implementations for Memory, LevelDB, and Redis.
Index ¶
- Variables
- type Config
- type LevelDBConfig
- type LevelDBStore
- func (l *LevelDBStore) Close() error
- func (l *LevelDBStore) Count(ctx context.Context, prefix string) (int, error)
- func (l *LevelDBStore) Delete(ctx context.Context, key string) error
- func (l *LevelDBStore) Exists(ctx context.Context, key string) (bool, error)
- func (l *LevelDBStore) Get(ctx context.Context, key string) ([]byte, error)
- func (l *LevelDBStore) List(ctx context.Context, keyPrefix string) ([]string, error)
- func (l *LevelDBStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type MemoryConfig
- type MemoryStore
- func (m *MemoryStore) Close() error
- func (m *MemoryStore) Count(ctx context.Context, prefix string) (int, error)
- func (m *MemoryStore) Delete(ctx context.Context, key string) error
- func (m *MemoryStore) Exists(ctx context.Context, key string) (bool, error)
- func (m *MemoryStore) Get(ctx context.Context, key string) ([]byte, error)
- func (m *MemoryStore) List(ctx context.Context, keyPrefix string) ([]string, error)
- func (m *MemoryStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type RedisConfig
- type RedisStore
- func (r *RedisStore) Close() error
- func (r *RedisStore) Count(ctx context.Context, prefix string) (int, error)
- func (r *RedisStore) Delete(ctx context.Context, key string) error
- func (r *RedisStore) Exists(ctx context.Context, key string) (bool, error)
- func (r *RedisStore) Get(ctx context.Context, key string) ([]byte, error)
- func (r *RedisStore) List(ctx context.Context, keyPrefix string) ([]string, error)
- func (r *RedisStore) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
- type Store
Constants ¶
This section is empty.
Variables ¶
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) Delete ¶
func (l *LevelDBStore) Delete(ctx context.Context, key string) error
Delete removes a key.
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) Delete ¶
func (m *MemoryStore) Delete(ctx context.Context, key string) error
Delete removes a key.
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) Delete ¶
func (r *RedisStore) Delete(ctx context.Context, key string) error
Delete removes a key.
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.