pub struct MockCache { /* private fields */ }Expand description
Mock cache implementation for testing purposes.
Provides an in-memory cache that mimics Redis behavior including:
- TTL expiration
- NX (not exists) semantics
- Error injection for testing error handling
§Example
use mx_cache::mock::MockCache;
use mx_cache::Cache;
use std::time::Duration;
let cache = MockCache::new();
cache.set(b"key", b"value", Duration::from_secs(60)).await.unwrap();
let result = cache.get(b"key").await.unwrap();
assert_eq!(result, Some(b"value".to_vec()));Implementations§
Source§impl MockCache
impl MockCache
Sourcepub fn with_data<I, K, V>(entries: I) -> Self
pub fn with_data<I, K, V>(entries: I) -> Self
Creates a mock cache with pre-populated data.
§Arguments
entries- Iterator of (key, value) pairs to pre-populate
Sourcepub fn enable_error_mode(&self, message: &str)
pub fn enable_error_mode(&self, message: &str)
Enables error mode - all subsequent operations will fail with the given message.
This is useful for testing error handling paths.
Sourcepub fn disable_error_mode(&self)
pub fn disable_error_mode(&self)
Disables error mode - operations will proceed normally.
Sourcepub fn operation_counts(&self) -> OperationCounts
pub fn operation_counts(&self) -> OperationCounts
Returns the current operation counts.
Sourcepub fn reset_counts(&self)
pub fn reset_counts(&self)
Resets operation counts to zero.
Sourcepub fn force_expire(&self, key: &[u8])
pub fn force_expire(&self, key: &[u8])
Manually expires an entry (for testing TTL behavior).
Trait Implementations§
Source§impl Cache for MockCache
impl Cache for MockCache
fn set_nx_px( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<bool>> + Send
fn set( &self, key: &[u8], value: &[u8], ttl: Duration, ) -> impl Future<Output = Result<()>> + Send
fn get( &self, key: &[u8], ) -> impl Future<Output = Result<Option<Vec<u8>>>> + Send
fn del(&self, key: &[u8]) -> impl Future<Output = Result<()>> + Send
Auto Trait Implementations§
impl Freeze for MockCache
impl RefUnwindSafe for MockCache
impl Send for MockCache
impl Sync for MockCache
impl Unpin for MockCache
impl UnsafeUnpin for MockCache
impl UnwindSafe for MockCache
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