pub struct MemoryCache { /* private fields */ }Expand description
In-memory LRU cache with TTL support
This cache provides ultra-fast local caching for frequently accessed data. It complements distributed caches (like Redis) by providing:
- Sub-microsecond latency (no network round trip)
- Automatic memory management via LRU eviction
- TTL-based expiration
- Thread-safe concurrent access
§Thread Safety
The cache uses Arc<RwLock<...>> for thread-safe access:
- Multiple concurrent reads (via read lock)
- Exclusive writes (via write lock)
- Clone is cheap (just increments Arc reference count)
§Size Management
The cache tracks size in bytes and enforces two limits:
- Total capacity: Sum of all cached values
- Max item size: Individual value size limit
When capacity is exceeded, items are evicted using probabilistic LRU.
Implementations§
Source§impl MemoryCache
impl MemoryCache
Sourcepub fn new(capacity: usize, max_item_size: usize) -> Self
pub fn new(capacity: usize, max_item_size: usize) -> Self
Create a new in-memory cache
§Parameters
capacity: Maximum total size in bytes (sum of all values)max_item_size: Maximum size of a single item in bytes
§Example
use warpdrive::cache::memory::MemoryCache;
// 32 MB cache with 1 MB max item size
let cache = MemoryCache::new(32 * 1024 * 1024, 1024 * 1024);Sourcepub fn stats(&self) -> (usize, usize, usize)
pub fn stats(&self) -> (usize, usize, usize)
Get cache statistics (for debugging/monitoring)
Returns current size, capacity, and item count.
§Example
let cache = MemoryCache::new(1024, 256);
let (size, capacity, count) = cache.stats();
println!("Cache: {}/{} bytes, {} items", size, capacity, count);Trait Implementations§
Source§impl Cache for MemoryCache
impl Cache for MemoryCache
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Option<Vec<u8>>>> + 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 = Result<Option<Vec<u8>>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Retrieve a value from cache by key Read more
Source§fn set<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: &'life2 [u8],
ttl_seconds: u64,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
fn set<'life0, 'life1, 'life2, 'async_trait>(
&'life0 self,
key: &'life1 str,
value: &'life2 [u8],
ttl_seconds: u64,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
Store a value in cache with TTL Read more
Source§impl Clone for MemoryCache
impl Clone for MemoryCache
Source§fn clone(&self) -> MemoryCache
fn clone(&self) -> MemoryCache
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 MemoryCache
impl !RefUnwindSafe for MemoryCache
impl Send for MemoryCache
impl Sync for MemoryCache
impl Unpin for MemoryCache
impl !UnwindSafe for MemoryCache
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