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>
impl<B: CacheBackend> CacheExpander<B>
Sourcepub fn with_metrics(self, metrics: Box<dyn CacheMetrics>) -> Self
pub fn with_metrics(self, metrics: Box<dyn CacheMetrics>) -> Self
Set custom metrics handler.
Sourcepub fn with_ttl_policy(self, policy: TtlPolicy) -> Self
pub fn with_ttl_policy(self, policy: TtlPolicy) -> Self
Set custom TTL policy.
Sourcepub async fn with<T, F, R>(
&self,
feeder: &mut F,
repository: &R,
strategy: CacheStrategy,
) -> Result<()>
pub async fn with<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, ) -> Result<()>
Generic cache operation with strategy.
This is the primary method used in 80% of cases.
§Arguments
feeder: Entity feeder (implementsCacheFeed<T>)repository: Data repository (implementsDataRepository<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 failsError::DeserializationError: Cached data is corrupted or has wrong formatError::InvalidCacheEntry: Cache magic header mismatch or invalid envelopeError::VersionMismatch: Schema version mismatch between code and cached dataError::BackendError: Cache backend is unavailable or network errorError::RepositoryError: Database access failsError::Timeout: Operation exceeds timeout thresholdError::SerializationError: Entity serialization for caching fails
Sourcepub async fn with_config<T, F, R>(
&self,
feeder: &mut F,
repository: &R,
strategy: CacheStrategy,
config: OperationConfig,
) -> Result<()>
pub async fn with_config<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, config: OperationConfig, ) -> Result<()>
Execute cache operation with custom configuration.
This method allows per-operation overrides for TTL and retry logic.
§Arguments
feeder: Entity feeder (implementsCacheFeed<T>)repository: Data repository (implementsDataRepository<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 failsError::DeserializationError: Cached data is corrupted or has wrong formatError::InvalidCacheEntry: Cache magic header mismatch or invalid envelopeError::VersionMismatch: Schema version mismatch between code and cached dataError::BackendError: Cache backend is unavailable or network errorError::RepositoryError: Database access failsError::Timeout: Operation exceeds timeout thresholdError::SerializationError: Entity serialization for caching fails
Failed operations are retried up to config.retry_count times with exponential backoff.
Sourcepub fn backend_mut(&mut self) -> &mut B
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> 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