pub struct Keyspace { /* private fields */ }Expand description
The core key-value store.
All operations are single-threaded per shard — no internal locking. Memory usage is tracked incrementally on every mutation.
Implementations§
Source§impl Keyspace
impl Keyspace
Sourcepub fn with_config(config: ShardConfig) -> Self
pub fn with_config(config: ShardConfig) -> Self
Creates a new, empty keyspace with the given config.
Sourcepub fn get(&mut self, key: &str) -> Option<Value>
pub fn get(&mut self, key: &str) -> Option<Value>
Retrieves the value for key, or None if the key doesn’t exist
or has expired.
Expired keys are removed lazily on access. Successful reads update the entry’s last access time for LRU tracking.
Sourcepub fn set(
&mut self,
key: String,
value: Bytes,
expire: Option<Duration>,
) -> SetResult
pub fn set( &mut self, key: String, value: Bytes, expire: Option<Duration>, ) -> SetResult
Stores a key-value pair. If the key already existed, the old entry (including any TTL) is replaced entirely.
expire sets an optional TTL as a duration from now.
Returns SetResult::OutOfMemory if the memory limit is reached
and the eviction policy is NoEviction. With AllKeysLru, this
will evict keys to make room before inserting.
Sourcepub fn del(&mut self, key: &str) -> bool
pub fn del(&mut self, key: &str) -> bool
Removes a key. Returns true if the key existed (and wasn’t expired).
Sourcepub fn expire(&mut self, key: &str, seconds: u64) -> bool
pub fn expire(&mut self, key: &str, seconds: u64) -> bool
Sets an expiration on an existing key. Returns true if the key
exists (and the TTL was set), false if the key doesn’t exist.
Sourcepub fn ttl(&mut self, key: &str) -> TtlResult
pub fn ttl(&mut self, key: &str) -> TtlResult
Returns the TTL status for a key, following Redis semantics:
Seconds(n)if the key has a TTLNoExpiryif the key exists without a TTLNotFoundif the key doesn’t exist
Sourcepub fn stats(&self) -> KeyspaceStats
pub fn stats(&self) -> KeyspaceStats
Returns aggregated stats for this keyspace.
All fields are tracked incrementally — this is O(1).
Sourcepub fn expire_sample(&mut self, count: usize) -> usize
pub fn expire_sample(&mut self, count: usize) -> usize
Randomly samples up to count keys and removes any that have expired.
Returns the number of keys actually removed. Used by the active expiration cycle to clean up keys that no one is reading.