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.
§Multi-Tier Support (v0.5.0+)
The builder now supports dynamic multi-tier architectures (L1+L2+L3+L4+…).
Use .with_tier() to add custom tiers, or .with_l3() / .with_l4() for convenience.
§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) - All tier backends must implement
L2CacheBackend(for TTL support) - Streaming backends must implement
StreamingBackend
§Example - Default 2-Tier
use multi_tier_cache::CacheSystemBuilder;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Use default backends (Moka + Redis)
let cache = CacheSystemBuilder::new()
.build()
.await?;
Ok(())
}§Example - Custom 3-Tier (v0.5.0+)
use multi_tier_cache::{CacheSystemBuilder, TierConfig};
use std::sync::Arc;
let l1 = Arc::new(L1Cache::new().await?);
let l2 = Arc::new(L2Cache::new().await?);
let l3 = Arc::new(RocksDBCache::new("/tmp/cache").await?);
let cache = CacheSystemBuilder::new()
.with_tier(l1, TierConfig::as_l1())
.with_tier(l2, TierConfig::as_l2())
.with_l3(l3) // Convenience method
.build()
.await?;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).
Use .with_tier() to configure multi-tier architecture (v0.5.0+).
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 fn with_tier(
self,
backend: Arc<dyn L2CacheBackend>,
config: TierConfig,
) -> Self
pub fn with_tier( self, backend: Arc<dyn L2CacheBackend>, config: TierConfig, ) -> Self
Configure a cache tier with custom settings (v0.5.0+)
Add a cache tier to the multi-tier architecture. Tiers will be sorted
by tier_level during build.
§Arguments
backend- Any type implementingL2CacheBackendtraitconfig- Tier configuration (level, promotion, TTL scale)
§Example
use multi_tier_cache::{CacheSystemBuilder, TierConfig, L1Cache, L2Cache};
use std::sync::Arc;
let l1 = Arc::new(L1Cache::new().await?);
let l2 = Arc::new(L2Cache::new().await?);
let l3 = Arc::new(RocksDBCache::new("/tmp").await?);
let cache = CacheSystemBuilder::new()
.with_tier(l1, TierConfig::as_l1())
.with_tier(l2, TierConfig::as_l2())
.with_tier(l3, TierConfig::as_l3())
.build()
.await?;Sourcepub fn with_l3(self, backend: Arc<dyn L2CacheBackend>) -> Self
pub fn with_l3(self, backend: Arc<dyn L2CacheBackend>) -> Self
Convenience method to add L3 cache tier (v0.5.0+)
Adds a cold storage tier with 2x TTL multiplier.
§Arguments
backend- L3 backend (e.g.,RocksDB,LevelDB)
§Example
use std::sync::Arc;
let rocksdb = Arc::new(RocksDBCache::new("/tmp/l3cache").await?);
let cache = CacheSystemBuilder::new()
.with_l3(rocksdb)
.build()
.await?;Sourcepub fn with_l4(self, backend: Arc<dyn L2CacheBackend>) -> Self
pub fn with_l4(self, backend: Arc<dyn L2CacheBackend>) -> Self
Convenience method to add L4 cache tier (v0.5.0+)
Adds an archive storage tier with 8x TTL multiplier.
§Arguments
backend- L4 backend (e.g., S3, file system)
§Example
use std::sync::Arc;
let s3_cache = Arc::new(S3Cache::new("my-bucket").await?);
let cache = CacheSystemBuilder::new()
.with_l4(s3_cache)
.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).
§Multi-Tier Mode (v0.5.0+)
If tiers were configured via .with_tier(), .with_l3(), or .with_l4(),
the builder creates a multi-tier CacheManager using new_with_tiers().
§Returns
Ok(CacheSystem)- Successfully constructed cache systemErr(e)- Failed to initialize backends (e.g., Redis connection error)
§Example - Default 2-Tier
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(())
}§Errors
Returns an error if the default backends cannot be initialized.