Documentation
¶
Overview ¶
Package store is RAMen's in-memory keyspace. Keys are spread across a fixed number of shards, each guarded by its own RWMutex, so unrelated keys rarely contend for the same lock. Expiry is handled both lazily (on access) and by a background sweep (see expiry.go). The PRD calls for a "sharded in-process map" and to "revisit only if benchmarks show real lock contention" (§9).
Index ¶
- Variables
- type Record
- type SetOptions
- type Store
- func (s *Store) Append(key, val string) (int, error)
- func (s *Store) DBSize() int
- func (s *Store) Del(keys ...string) int
- func (s *Store) Exists(keys ...string) int
- func (s *Store) Expire(key string, ttl time.Duration) bool
- func (s *Store) Export() []Record
- func (s *Store) Flush()
- func (s *Store) Get(key string) (val string, ok bool, err error)
- func (s *Store) GetRange(key string, start, end int64) (string, error)
- func (s *Store) GetSet(key, val string) (old string, hadOld bool, err error)
- func (s *Store) HDel(key string, fields ...string) (int, error)
- func (s *Store) HGet(key, field string) (string, bool, error)
- func (s *Store) HGetAll(key string) ([]string, error)
- func (s *Store) HKeys(key string) ([]string, error)
- func (s *Store) HLen(key string) (int, error)
- func (s *Store) HSet(key string, pairs map[string]string) (int, error)
- func (s *Store) HVals(key string) ([]string, error)
- func (s *Store) Import(recs []Record)
- func (s *Store) IncrBy(key string, delta int64) (int64, error)
- func (s *Store) Keys(pattern string) []string
- func (s *Store) LIndex(key string, idx int) (string, bool, error)
- func (s *Store) LLen(key string) (int, error)
- func (s *Store) LPop(key string) (string, bool, error)
- func (s *Store) LPush(key string, values ...string) (int, error)
- func (s *Store) LRange(key string, start, stop int) ([]string, error)
- func (s *Store) Persist(key string) bool
- func (s *Store) RPop(key string) (string, bool, error)
- func (s *Store) RPush(key string, values ...string) (int, error)
- func (s *Store) SAdd(key string, members ...string) (int, error)
- func (s *Store) SCard(key string) (int, error)
- func (s *Store) SIsMember(key, member string) (bool, error)
- func (s *Store) SMembers(key string) ([]string, error)
- func (s *Store) SRem(key string, members ...string) (int, error)
- func (s *Store) Set(key, val string, opts SetOptions) bool
- func (s *Store) SetRange(key string, offset int, val string) (int, error)
- func (s *Store) StartSweeper(ctx context.Context, interval time.Duration)
- func (s *Store) TTL(key string) (d time.Duration, hasTTL, ok bool)
- func (s *Store) Type(key string) string
- func (s *Store) VCard(key string) (int, error)
- func (s *Store) VDel(key, id string) (bool, error)
- func (s *Store) VDim(key string) (int, error)
- func (s *Store) VSearch(key string, query []float32, k int) ([]vector.Result, error)
- func (s *Store) VSet(key, id string, vec []float32, meta string) error
- func (s *Store) ZAdd(key string, members []ZMember) (int, error)
- func (s *Store) ZCard(key string) (int, error)
- func (s *Store) ZRange(key string, start, stop int) ([]ZMember, error)
- func (s *Store) ZRangeByScore(key string, min, max float64) ([]ZMember, error)
- func (s *Store) ZRem(key string, members ...string) (int, error)
- func (s *Store) ZScore(key, member string) (float64, bool, error)
- type VecRecord
- type ZMember
Constants ¶
This section is empty.
Variables ¶
var ErrNotInteger = errors.New("ERR value is not an integer or out of range")
ErrNotInteger is returned when a string value cannot be parsed as an int64 for INCR/DECR-style operations.
var ErrWrongType = errors.New("WRONGTYPE Operation against a key holding the wrong kind of value")
ErrWrongType mirrors Redis' WRONGTYPE condition: an operation was attempted against a key holding a different data type.
Functions ¶
This section is empty.
Types ¶
type Record ¶
type Record struct {
Key string
Type string // "string","hash","list","set","zset","vector"
ExpireAtUnixMs int64 // 0 == no expiry
Str string
Hash map[string]string
List []string
Set []string
ZSet []ZMember
Vectors []VecRecord
VecDim int
}
Record is the serialisable form of a single key, used by the persist package to snapshot and restore the keyspace. Exactly one of the type-specific fields is populated according to Type.
type SetOptions ¶
type SetOptions struct {
TTL time.Duration // 0 means no expiry
HasEx bool // an EX/PX flag was supplied
NX bool // only set if the key does not exist
XX bool // only set if the key already exists
}
SetOptions controls SET behaviour (EX/PX/NX/XX flags).
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the full keyspace, safe for concurrent use.
func (*Store) Append ¶
Append concatenates val to the string at key (creating it if absent) and returns the new length.
func (*Store) Expire ¶
Expire sets a relative TTL in milliseconds on an existing key. It reports whether the key existed.
func (*Store) Export ¶
Export returns a snapshot of every live key. It is safe to call while the store is serving traffic; each shard is read-locked in turn.
func (*Store) GetRange ¶
GetRange returns the substring between start and end (inclusive), with Redis-style negative offsets and clamping. A missing key yields an empty string.
func (*Store) HDel ¶
HDel removes fields and returns how many were removed. The key is dropped when its last field is deleted.
func (*Store) HSet ¶
HSet sets the given field/value pairs on the hash at key (creating it if absent) and returns the number of newly created fields.
func (*Store) Import ¶
Import loads records into the store, replacing any existing data. Records whose expiry is already in the past are skipped.
func (*Store) IncrBy ¶
IncrBy adds delta to the integer string at key (treating a missing key as 0) and returns the new value.
func (*Store) LRange ¶
LRange returns the elements in the inclusive index range [start, stop], where negative indices count from the tail (Redis semantics).
func (*Store) SAdd ¶
SAdd adds members to the set at key (creating it if absent) and returns how many were newly added.
func (*Store) SRem ¶
SRem removes members and returns how many were removed; the key is dropped when emptied.
func (*Store) Set ¶
func (s *Store) Set(key, val string, opts SetOptions) bool
Set assigns a string value to key, honouring the supplied options. It reports whether the write happened (NX/XX can suppress it).
func (*Store) SetRange ¶
SetRange overwrites from offset with val, zero-padding past the current end. It returns the length of the string after the write.
func (*Store) StartSweeper ¶
StartSweeper runs a background goroutine that periodically scans shards and deletes expired keys. Lazy expiry already removes keys on access; the sweep reclaims memory for keys that are never read again. It stops when ctx is cancelled.
func (*Store) TTL ¶
TTL returns the remaining time to live for key. ok is false when the key does not exist; hasTTL is false when the key exists but is persistent.
func (*Store) Type ¶
Type returns the Redis type name of key ("string", "hash", "list", "set", "zset", "vector") or "none" if it does not exist.
func (*Store) ZAdd ¶
ZAdd sets the score for each member (creating the set if absent) and returns the number of newly added members.
func (*Store) ZRange ¶
ZRange returns members in the inclusive rank range [start, stop] ordered by score (negative indices count from the end).
func (*Store) ZRangeByScore ¶
ZRangeByScore returns members whose score lies in [min, max] inclusive, ordered by score.