pub struct InMemoryRepository<T: CacheEntity> { /* private fields */ }Expand description
Simple in-memory repository for testing cache-kit implementations.
Provides a straightforward mock DataRepository suitable for unit tests
where you want to control what data is “stored” without setting up a real database.
§Why Use InMemoryRepository
- Fast Tests: No database setup, teardown, or network calls
- Deterministic: Control exactly what data is present
- Isolated: Each test can have its own data without conflicts
- Simple: Easy to understand test behavior and debug failures
§Example Usage
ⓘ
#[tokio::test]
async fn test_cache_expander_with_mock_data() {
// Create and populate mock repository
let mut repo = InMemoryRepository::new();
repo.insert("user:1".to_string(), my_user_entity);
// Use with cache-kit components
let mut feeder = MyFeeder::new();
let result = expander.with(&mut feeder, &repo, CacheStrategy::Refresh).await;
assert!(result.is_ok());
}§Testing Different Scenarios
- Cache hit: Populate repo, cache will find the data
- Cache miss: Keep repo empty, cache will fallback to repo (which has nothing)
- Invalidation: Clear repo between operations to test refresh behavior
- Batch operations: Use
fetch_by_ids()to test multi-key scenarios
Implementations§
Trait Implementations§
Source§impl<T: CacheEntity> DataRepository<T> for InMemoryRepository<T>
impl<T: CacheEntity> DataRepository<T> for InMemoryRepository<T>
Source§async fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>
async fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>
Fetch entity by ID from primary data source. Read more
Source§async fn fetch_by_ids(&self, ids: &[T::Key]) -> Result<Vec<Option<T>>>
async fn fetch_by_ids(&self, ids: &[T::Key]) -> Result<Vec<Option<T>>>
Batch fetch entities by IDs (optional optimization). Read more
Source§impl<T: CacheEntity> Default for InMemoryRepository<T>
impl<T: CacheEntity> Default for InMemoryRepository<T>
Auto Trait Implementations§
impl<T> Freeze for InMemoryRepository<T>
impl<T> RefUnwindSafe for InMemoryRepository<T>where
T: RefUnwindSafe,
impl<T> Send for InMemoryRepository<T>
impl<T> Sync for InMemoryRepository<T>
impl<T> Unpin for InMemoryRepository<T>where
T: Unpin,
impl<T> UnwindSafe for InMemoryRepository<T>where
T: UnwindSafe,
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