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
impl InMemoryBackend
Sourcepub async fn stats(&self) -> CacheStats
pub async fn stats(&self) -> CacheStats
Get memory statistics.
Trait Implementations§
Source§impl CacheBackend for InMemoryBackend
impl CacheBackend for InMemoryBackend
Source§async fn get(&self, key: &str) -> Result<Option<Vec<u8>>>
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<()>
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 exists(&self, key: &str) -> Result<bool>
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>>>>
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<()>
async fn mdelete(&self, keys: &[&str]) -> Result<()>
Bulk delete operation (optional optimization). Read more
Source§impl Clone for InMemoryBackend
impl Clone for InMemoryBackend
Source§fn clone(&self) -> InMemoryBackend
fn clone(&self) -> InMemoryBackend
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for InMemoryBackend
impl !RefUnwindSafe for InMemoryBackend
impl Send for InMemoryBackend
impl Sync for InMemoryBackend
impl Unpin for InMemoryBackend
impl !UnwindSafe for InMemoryBackend
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more