InMemoryBackend

Struct InMemoryBackend 

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

Thread-safe async in-memory cache backend.

Uses DashMap for lock-free concurrent access with fine-grained per-key sharding. No async locks required - operations are non-blocking. Automatically handles TTL expiration on access.

§Example

use cache_kit::backend::{InMemoryBackend, CacheBackend};
use std::time::Duration;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let backend = InMemoryBackend::new();

    // Store data
    backend.set("key1", b"value".to_vec(), None).await?;

    // Retrieve data
    let value = backend.get("key1").await?;
    assert!(value.is_some());

    // Store with TTL
    backend.set("key2", b"expires".to_vec(), Some(Duration::from_secs(300))).await?;

    Ok(())
}

Implementations§

Source§

impl InMemoryBackend

Source

pub fn new() -> Self

Create a new in-memory cache backend.

Source

pub async fn len(&self) -> usize

Get the current number of entries in cache.

Source

pub async fn is_empty(&self) -> bool

Check if cache is empty.

Source

pub async fn stats(&self) -> CacheStats

Get memory statistics.

Source

pub async fn log_stats(&self)

Print cache statistics to debug log.

Trait Implementations§

Source§

impl CacheBackend for InMemoryBackend

Source§

async fn get(&self, key: &str) -> Result<Option<Vec<u8>>>

Retrieve value from cache by key. Read more
Source§

async fn set( &self, key: &str, value: Vec<u8>, ttl: Option<Duration>, ) -> Result<()>

Store value in cache with optional TTL. Read more
Source§

async fn delete(&self, key: &str) -> Result<()>

Remove value from cache. Read more
Source§

async fn exists(&self, key: &str) -> Result<bool>

Check if key exists in cache (optional optimization). Read more
Source§

async fn mget(&self, keys: &[&str]) -> Result<Vec<Option<Vec<u8>>>>

Bulk get operation (optional optimization). Read more
Source§

async fn mdelete(&self, keys: &[&str]) -> Result<()>

Bulk delete operation (optional optimization). Read more
Source§

async fn health_check(&self) -> Result<bool>

Health check - verify backend is accessible. Read more
Source§

async fn clear_all(&self) -> Result<()>

Optional: Clear all cache (use with caution). Read more
Source§

impl Clone for InMemoryBackend

Source§

fn clone(&self) -> InMemoryBackend

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for InMemoryBackend

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.