pub struct CacheService<B: CacheBackend> { /* private fields */ }Expand description
High-level cache service for web applications.
Wraps CacheExpander in Arc for easy sharing across threads without
requiring external Arc<Mutex<>> wrappers.
§Design
Since CacheBackend implementations use interior mutability (RwLock, Mutex),
and CacheExpander now uses &self methods, we can safely wrap it in Arc
without needing an additional Mutex.
§Example
use cache_kit::{CacheService, backend::InMemoryBackend};
// Create service (can be shared across threads)
let cache = CacheService::new(InMemoryBackend::new());
// In your web service struct
pub struct UserService {
cache: CacheService<InMemoryBackend>,
repo: Arc<UserRepository>,
}
impl UserService {
pub fn get(&self, id: &str) -> Result<Option<User>> {
let mut feeder = UserFeeder { id: id.to_string(), user: None };
self.cache.execute(&mut feeder, &*self.repo, CacheStrategy::Refresh)?;
Ok(feeder.user)
}
}Implementations§
Source§impl<B: CacheBackend> CacheService<B>
impl<B: CacheBackend> CacheService<B>
Sourcepub fn with_metrics(backend: B, metrics: Box<dyn CacheMetrics>) -> Self
pub fn with_metrics(backend: B, metrics: Box<dyn CacheMetrics>) -> Self
Create a new cache service with custom metrics.
Sourcepub async fn execute<T, F, R>(
&self,
feeder: &mut F,
repository: &R,
strategy: CacheStrategy,
) -> Result<()>
pub async fn execute<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, ) -> Result<()>
Execute a cache operation.
This is equivalent to calling expander.with() but more ergonomic
for service-oriented architectures.
§Arguments
feeder: Entity feeder (implementsCacheFeed<T>)repository: Data repository (implementsDataRepository<T>)strategy: Cache strategy (Fresh, Refresh, Invalidate, Bypass)
§Example
let cache = CacheService::new(InMemoryBackend::new());
let mut feeder = UserFeeder { id: "user_123".to_string(), user: None };
let repo = UserRepository::new(pool);
cache.execute(&mut feeder, &repo, CacheStrategy::Refresh).await?;
let user = feeder.user;§Errors
Returns Err in these cases:
Error::ValidationError: Feeder validation failsError::DeserializationError: Cached data is corruptedError::InvalidCacheEntry: Invalid cache envelopeError::VersionMismatch: Schema version mismatchError::BackendError: Cache backend unavailableError::RepositoryError: Database access failsError::SerializationError: Entity serialization fails
Sourcepub async fn execute_with_config<T, F, R>(
&self,
feeder: &mut F,
repository: &R,
strategy: CacheStrategy,
config: OperationConfig,
) -> Result<()>
pub async fn execute_with_config<T, F, R>( &self, feeder: &mut F, repository: &R, strategy: CacheStrategy, config: OperationConfig, ) -> Result<()>
Execute a cache operation with advanced configuration.
This method allows per-operation configuration such as TTL override and retry logic, while working seamlessly with Arc-wrapped services.
§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
let cache = CacheService::new(InMemoryBackend::new());
let mut feeder = UserFeeder { id: "user_123".to_string(), user: None };
let repo = UserRepository::new(pool);
let config = OperationConfig::default()
.with_ttl(Duration::from_secs(300))
.with_retry(3);
cache.execute_with_config(&mut feeder, &repo, CacheStrategy::Refresh, config).await?;
let user = feeder.user;§Errors
Same error cases as execute(), plus retry-related failures.
Sourcepub fn expander(&self) -> &CacheExpander<B>
pub fn expander(&self) -> &CacheExpander<B>
Get a reference to the underlying expander.
Use this if you need direct access to expander methods.
Trait Implementations§
Source§impl<B: Clone + CacheBackend> Clone for CacheService<B>
impl<B: Clone + CacheBackend> Clone for CacheService<B>
Source§fn clone(&self) -> CacheService<B>
fn clone(&self) -> CacheService<B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more