pub struct CacheSystemBuilder { /* private fields */ }Expand description
Builder for constructing CacheSystem with custom backends
This builder allows you to configure custom L1 (in-memory) and L2 (distributed) cache backends, enabling you to swap Moka and Redis with alternative implementations.
§Default Behavior
If no custom backends are provided, the builder uses:
- L1: Moka in-memory cache
- L2: Redis distributed cache
§Type Safety
The builder accepts any type that implements the required traits:
- L1 backends must implement
CacheBackend - L2 backends must implement
L2CacheBackend(extendsCacheBackend) - Streaming backends must implement
StreamingBackend
§Example
use multi_tier_cache::CacheSystemBuilder;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Use default backends (Moka + Redis)
let cache = CacheSystemBuilder::new()
.build()
.await?;
Ok(())
}Implementations§
Source§impl CacheSystemBuilder
impl CacheSystemBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new builder with no custom backends configured
By default, calling .build() will use Moka (L1) and Redis (L2).
Sourcepub fn with_l1(self, backend: Arc<dyn CacheBackend>) -> Self
pub fn with_l1(self, backend: Arc<dyn CacheBackend>) -> Self
Configure a custom L1 (in-memory) cache backend
§Arguments
backend- Any type implementingCacheBackendtrait
§Example
use std::sync::Arc;
use multi_tier_cache::CacheSystemBuilder;
let custom_l1 = Arc::new(MyCustomL1::new());
let cache = CacheSystemBuilder::new()
.with_l1(custom_l1)
.build()
.await?;Sourcepub fn with_l2(self, backend: Arc<dyn L2CacheBackend>) -> Self
pub fn with_l2(self, backend: Arc<dyn L2CacheBackend>) -> Self
Configure a custom L2 (distributed) cache backend
§Arguments
backend- Any type implementingL2CacheBackendtrait
§Example
use std::sync::Arc;
use multi_tier_cache::CacheSystemBuilder;
let custom_l2 = Arc::new(MyMemcachedBackend::new());
let cache = CacheSystemBuilder::new()
.with_l2(custom_l2)
.build()
.await?;Sourcepub fn with_streams(self, backend: Arc<dyn StreamingBackend>) -> Self
pub fn with_streams(self, backend: Arc<dyn StreamingBackend>) -> Self
Configure a custom streaming backend
This is optional. If not provided, streaming functionality will use
the L2 backend if it implements StreamingBackend.
§Arguments
backend- Any type implementingStreamingBackendtrait
§Example
use std::sync::Arc;
use multi_tier_cache::CacheSystemBuilder;
let kafka_backend = Arc::new(MyKafkaBackend::new());
let cache = CacheSystemBuilder::new()
.with_streams(kafka_backend)
.build()
.await?;Sourcepub async fn build(self) -> Result<CacheSystem>
pub async fn build(self) -> Result<CacheSystem>
Build the CacheSystem with configured or default backends
If no custom backends were provided via .with_l1() or .with_l2(),
this method creates default backends (Moka for L1, Redis for L2).
§Returns
Ok(CacheSystem)- Successfully constructed cache systemErr(e)- Failed to initialize backends (e.g., Redis connection error)
§Note
Currently, custom backends are stored but not yet used by CacheManager. Full trait-based backend support will be available in the next phase. For now, this builder ensures API stability for future upgrades.
§Example
use multi_tier_cache::CacheSystemBuilder;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cache = CacheSystemBuilder::new()
.build()
.await?;
// Use cache_manager for operations
let manager = cache.cache_manager();
Ok(())
}