kvstore

package
v1.2.34 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrClosed     = errors.New("kvstore is closed")
	ErrEmptyKey   = errors.New("key cannot be empty")
	ErrNotFound   = errors.New("key not found")
	ErrInvalidTTL = errors.New("ttl cannot be negative")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// CleanupInterval controls how often expired entries are removed.
	// If <= 0, defaults to 30s.
	CleanupInterval time.Duration
	// DefaultTTL applies when Set is used.
	// A zero value means entries do not expire by default.
	DefaultTTL time.Duration
}

Config controls in-memory KV store behavior.

type Store

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

Store is an in-memory KV store with optional TTL support.

func New

func New(cfg Config) (*Store, error)

New creates a new in-memory KV store.

func (*Store) Close

func (s *Store) Close() error

Close stops background cleanup and prevents further operations.

func (*Store) Delete

func (s *Store) Delete(key string) (bool, error)

Delete removes a key.

func (*Store) DeleteRemote

func (s *Store) DeleteRemote(key string, deletedAt int64) error

DeleteRemote applies a remotely-gossiped delete without triggering OnDelete. deletedAt is the absolute Unix nanosecond timestamp when the delete was issued. The delete is skipped if the local entry was written after the delete intent (last-write-wins).

func (*Store) Get

func (s *Store) Get(key string) (any, error)

Get retrieves a value by key.

func (*Store) GetAndDelete

func (s *Store) GetAndDelete(key string) (any, error)

GetAndDelete retrieves and deletes a key atomically.

func (*Store) Len

func (s *Store) Len() int

Len returns the number of currently non-expired keys.

func (*Store) RegisterDecoder

func (s *Store) RegisterDecoder(keyPrefix string, decoder TypeDecoder)

RegisterDecoder registers a decoder for keys matching the given prefix. Used by the receiving side to reconstruct concrete types from gossip payloads.

func (*Store) Set

func (s *Store) Set(key string, value any) error

Set stores a value using the store's default TTL.

func (*Store) SetDelegate

func (s *Store) SetDelegate(d SyncDelegate)

SetDelegate plugs in the cluster sync implementation.

func (*Store) SetNXWithTTL

func (s *Store) SetNXWithTTL(key string, value any, ttl time.Duration) (bool, error)

SetNXWithTTL atomically sets a value with TTL only if the key does not exist. Returns true if the key was set, false if the key already existed. ttl=0 means no expiration.

func (*Store) SetRemote

func (s *Store) SetRemote(key string, valueJSON []byte, writtenAt int64, expiresAt int64) error

SetRemote applies a remotely-gossiped entry without triggering OnSet. writtenAt and expiresAt must be absolute Unix nanosecond timestamps. If the local entry was written more recently than writtenAt the update is silently skipped (last-write-wins by wall clock on the writing node).

func (*Store) SetWithTTL

func (s *Store) SetWithTTL(key string, value any, ttl time.Duration) error

SetWithTTL stores a value with an explicit TTL. ttl=0 means no expiration.

type SyncDelegate

type SyncDelegate interface {
	OnSet(key string, valueJSON []byte, writtenAt int64, expiresAt int64)
	OnDelete(key string, deletedAt int64)
}

SyncDelegate is notified of all mutations, enabling cross-node replication. All calls happen synchronously after the local mutation has succeeded. writtenAt / deletedAt are absolute Unix nanosecond timestamps used by remote nodes for last-write-wins conflict resolution. expiresAt is an absolute Unix nanosecond timestamp; 0 means no expiration.

type TypeDecoder

type TypeDecoder func(data []byte) (any, error)

TypeDecoder reconstructs a concrete value from its JSON representation. Register decoders by key prefix via RegisterDecoder.

Jump to

Keyboard shortcuts

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