pub struct DashMapCache { /* private fields */ }Expand description
Simple concurrent cache using DashMap
Use Case: Educational reference, simple concurrent scenarios
Features:
- Lock-free concurrent reads/writes
- Manual TTL tracking
- No automatic eviction (manual cleanup needed)
- Minimal memory overhead
Limitations:
- No automatic eviction policy (LRU, LFU, etc.)
- No size limits (unbounded growth)
- Manual TTL cleanup required
When to use:
- Learning how to implement cache backends
- Simple use cases with predictable data sizes
- When you need full control over eviction logic
Example:
use multi_tier_cache::backends::DashMapCache;
use multi_tier_cache::traits::CacheBackend;
use std::time::Duration;
let cache = DashMapCache::new();
let value = serde_json::json!({"user": "alice"});
cache.set_with_ttl("user:1", value.clone(), Duration::from_secs(60)).await?;
let cached = cache.get("user:1").await;
assert_eq!(cached, Some(value));Implementations§
Source§impl DashMapCache
impl DashMapCache
Sourcepub fn cleanup_expired(&self) -> usize
pub fn cleanup_expired(&self) -> usize
Cleanup expired entries (should be called periodically)
Note: DashMap doesn’t have automatic eviction, so you need to
call this method periodically to remove expired entries.
Trait Implementations§
Source§impl CacheBackend for DashMapCache
Implement CacheBackend trait for DashMapCache
impl CacheBackend for DashMapCache
Implement CacheBackend trait for DashMapCache
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Option<Value>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Get value from cache by key Read more
Source§fn set_with_ttl<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn set_with_ttl<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: Value,
ttl: Duration,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Set value in cache with time-to-live Read more
Source§fn remove<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn remove<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Remove value from cache Read more
Auto Trait Implementations§
impl Freeze for DashMapCache
impl !RefUnwindSafe for DashMapCache
impl Send for DashMapCache
impl Sync for DashMapCache
impl Unpin for DashMapCache
impl !UnwindSafe for DashMapCache
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more