InMemoryRepository

Struct InMemoryRepository 

Source
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§

Source§

impl<T: CacheEntity> InMemoryRepository<T>

Source

pub fn new() -> Self

Create a new empty in-memory repository.

§Example
let repo = InMemoryRepository::<MyEntity>::new();
assert!(repo.is_empty());
Source

pub fn insert(&mut self, id: T::Key, value: T)

Insert or update an entity by key.

§Example
repo.insert("user:123".to_string(), my_user);
let found = repo.fetch_by_id(&"user:123".to_string()).await?;
Source

pub fn clear(&mut self)

Remove all entities from the repository.

Useful for resetting state between test cases.

§Example
repo.insert("user:1".to_string(), entity);
assert_eq!(repo.len(), 1);
repo.clear();
assert!(repo.is_empty());
Source

pub fn len(&self) -> usize

Return the number of entities in the repository.

Source

pub fn is_empty(&self) -> bool

Return true if the repository contains no entities.

Trait Implementations§

Source§

impl<T: CacheEntity> DataRepository<T> for InMemoryRepository<T>

Source§

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>>>

Batch fetch entities by IDs (optional optimization). Read more
Source§

async fn count(&self) -> Result<u64>

Count total entities (optional, for statistics). Read more
Source§

async fn fetch_all(&self) -> Result<Vec<T>>

Optional: Get all entities (use sparingly, potentially large result). Read more
Source§

impl<T: CacheEntity> Default for InMemoryRepository<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.