CacheExpander

Struct CacheExpander 

Source
pub struct CacheExpander<B: CacheBackend> { /* private fields */ }
Expand description

Core cache expander - handles cache lookup and fallback logic.

This is the main entry point for cache operations. Supports multiple access patterns through different methods.

§Example

use cache_kit::{CacheExpander, backend::InMemoryBackend};

let backend = InMemoryBackend::new();
let mut expander = CacheExpander::new(backend);

Implementations§

Source§

impl<B: CacheBackend> CacheExpander<B>

Source

pub fn new(backend: B) -> Self

Create new expander with given backend.

Source

pub fn with_metrics(self, metrics: Box<dyn CacheMetrics>) -> Self

Set custom metrics handler.

Source

pub fn with_ttl_policy(self, policy: TtlPolicy) -> Self

Set custom TTL policy.

Source

pub async fn with<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, ) -> Result<()>
where T: CacheEntity, F: CacheFeed<T>, R: DataRepository<T>, T::Key: FromStr,

Generic cache operation with strategy.

This is the primary method used in 80% of cases.

§Arguments
  • feeder: Entity feeder (implements CacheFeed<T>)
  • repository: Data repository (implements DataRepository<T>)
  • strategy: Cache strategy (Fresh, Refresh, Invalidate, Bypass)
§Example
let expander = CacheExpander::new(redis_backend);
let mut feeder = EmploymentFeeder { id: "emp_123", employment: None };
let repo = EmploymentRepository { db: pool };

expander.with(
    &mut feeder,
    &repo,
    CacheStrategy::Refresh
).await?;

let employment = feeder.employment;
§Errors

Returns Err in these cases:

  • Error::ValidationError: Feeder or entity validation fails
  • Error::DeserializationError: Cached data is corrupted or has wrong format
  • Error::InvalidCacheEntry: Cache magic header mismatch or invalid envelope
  • Error::VersionMismatch: Schema version mismatch between code and cached data
  • Error::BackendError: Cache backend is unavailable or network error
  • Error::RepositoryError: Database access fails
  • Error::Timeout: Operation exceeds timeout threshold
  • Error::SerializationError: Entity serialization for caching fails
Source

pub async fn with_config<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, config: OperationConfig, ) -> Result<()>
where T: CacheEntity, F: CacheFeed<T>, R: DataRepository<T>, T::Key: FromStr,

Execute cache operation with custom configuration.

This method allows per-operation overrides for TTL and retry logic.

§Arguments
  • feeder: Entity feeder (implements CacheFeed<T>)
  • repository: Data repository (implements DataRepository<T>)
  • strategy: Cache strategy (Fresh, Refresh, Invalidate, Bypass)
  • config: Operation configuration (TTL override, retry count)
§Example
use cache_kit::{OperationConfig, CacheStrategy};
use std::time::Duration;

let config = OperationConfig::default()
    .with_ttl(Duration::from_secs(300))
    .with_retry(3);

expander.with_config(
    &mut feeder,
    &repo,
    CacheStrategy::Refresh,
    config
).await?;
§Errors

Returns Err in these cases:

  • Error::ValidationError: Feeder or entity validation fails
  • Error::DeserializationError: Cached data is corrupted or has wrong format
  • Error::InvalidCacheEntry: Cache magic header mismatch or invalid envelope
  • Error::VersionMismatch: Schema version mismatch between code and cached data
  • Error::BackendError: Cache backend is unavailable or network error
  • Error::RepositoryError: Database access fails
  • Error::Timeout: Operation exceeds timeout threshold
  • Error::SerializationError: Entity serialization for caching fails

Failed operations are retried up to config.retry_count times with exponential backoff.

Source

pub fn backend(&self) -> &B

Get backend reference (for advanced use).

Source

pub fn backend_mut(&mut self) -> &mut B

Get mutable backend reference (for advanced use).

Auto Trait Implementations§

§

impl<B> Freeze for CacheExpander<B>
where B: Freeze,

§

impl<B> !RefUnwindSafe for CacheExpander<B>

§

impl<B> Send for CacheExpander<B>

§

impl<B> Sync for CacheExpander<B>

§

impl<B> Unpin for CacheExpander<B>
where B: Unpin,

§

impl<B> !UnwindSafe for CacheExpander<B>

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.