MemoryStorage

Struct MemoryStorage 

Source
pub struct MemoryStorage { /* private fields */ }
Expand description

Main storage engine implementing Redis-like functionality

Provides thread-safe storage with transaction support and caching. All keys are case-insensitive and stored in lowercase.

Implementations§

Source§

impl MemoryStorage

Source

pub fn new() -> Self

Creates a new empty storage instance with default cache settings

Source

pub fn save_snapshot(&self, path: &str) -> Result<()>

Saves the current storage state to a file

§Arguments
  • path - Path to save the snapshot file
Source

pub fn load_snapshot(&mut self, path: &str) -> Result<()>

Loads storage state from a snapshot file

§Arguments
  • path - Path to the snapshot file to load
Source

pub fn start_transaction(&mut self)

Starts a new transaction

Creates a new transaction layer that will track changes until committed or rolled back.

Source

pub fn commit_transaction(&mut self) -> Result<Vec<String>, String>

Commits the current transaction

§Returns
  • Ok(Vec<String>) - Results of operations in the transaction
  • Err(String) - Error message if no transaction is active
Source

pub fn rollback_transaction(&mut self) -> Result<(), String>

Rolls back the current transaction

§Returns
  • Ok(()) - Transaction successfully rolled back
  • Err(String) - Error message if no transaction is active
Source

pub fn set(&mut self, key: String, value: String)

Sets a key-value pair in the storage

If a transaction is active, the change is recorded in the current transaction layer. Otherwise, it’s applied directly to the main storage. The value is also cached.

§Arguments
  • key - The key (case-insensitive)
  • value - The value to store
Source

pub fn get(&mut self, key: &str) -> Option<String>

Retrieves a value by its key

Checks the cache first, then active transactions from newest to oldest, finally falling back to main storage. Found values are cached for future access.

§Arguments
  • key - The key to look up (case-insensitive)
§Returns
  • Some(String) - The value if found
  • None - If the key doesn’t exist
Source

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

Deletes a key-value pair from storage

In a transaction, marks the key for deletion. Otherwise, removes it from main storage immediately. Also removes the key from cache if it existed.

§Arguments
  • key - The key to delete (case-insensitive)
§Returns

true if the key existed and was marked for deletion or removed

Source

pub fn incr(&mut self, key: &str) -> i64

Increments the numeric value stored at the given key

If the key doesn’t exist, it’s initialized with “0” before incrementing. Non-numeric values are treated as 0.

§Arguments
  • key - The key storing the numeric value (case-insensitive)
§Returns

The new value after incrementing

Source

pub fn decr(&mut self, key: &str) -> i64

Decrements the numeric value stored at the given key

If the key doesn’t exist, it’s initialized with “0” before decrementing. Non-numeric values are treated as 0.

§Arguments
  • key - The key storing the numeric value (case-insensitive)
§Returns

The new value after decrementing

Source

pub fn lpush(&mut self, key: &str, value: String) -> usize

Pushes a value to the front of a list

Creates the list if it doesn’t exist.

§Arguments
  • key - The list’s key (case-insensitive)
  • value - The value to push
§Returns

The new length of the list

Source

pub fn rpush(&mut self, key: &str, value: String) -> usize

Pushes a value to the end of a list

Creates the list if it doesn’t exist.

§Arguments
  • key - The list’s key (case-insensitive)
  • value - The value to push
§Returns

The new length of the list

Source

pub fn lpop(&mut self, key: &str) -> Option<String>

Removes and returns the first element from a list

§Arguments
  • key - The list’s key (case-insensitive)
§Returns
  • Some(String) - The removed value
  • None - If the list is empty or doesn’t exist
Source

pub fn rpop(&mut self, key: &str) -> Option<String>

Removes and returns the last element from a list

§Arguments
  • key - The list’s key (case-insensitive)
§Returns
  • Some(String) - The removed value
  • None - If the list is empty or doesn’t exist
Source

pub fn llen(&self, key: &str) -> usize

Returns the length of a list

If in a transaction, returns the length from the most recent transaction layer that has the list. Otherwise, returns the length from main storage.

§Arguments
  • key - The list’s key (case-insensitive)
§Returns

The length of the list, or 0 if it doesn’t exist

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.