Skip to main content

Keyspace

Struct Keyspace 

Source
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

Source

pub fn new() -> Self

Creates a new, empty keyspace with default config (no memory limit).

Source

pub fn with_config(config: ShardConfig) -> Self

Creates a new, empty keyspace with the given config.

Source

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.

Source

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.

Source

pub fn del(&mut self, key: &str) -> bool

Removes a key. Returns true if the key existed (and wasn’t expired).

Source

pub fn exists(&mut self, key: &str) -> bool

Returns true if the key exists and hasn’t expired.

Source

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.

Source

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 TTL
  • NoExpiry if the key exists without a TTL
  • NotFound if the key doesn’t exist
Source

pub fn stats(&self) -> KeyspaceStats

Returns aggregated stats for this keyspace.

All fields are tracked incrementally — this is O(1).

Source

pub fn len(&self) -> usize

Returns the number of live keys.

Source

pub fn is_empty(&self) -> bool

Returns true if the keyspace has no entries.

Source

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.

Trait Implementations§

Source§

impl Default for Keyspace

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V