CacheSystemBuilder

Struct CacheSystemBuilder 

Source
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 (extends CacheBackend)
  • 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

Source

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+).

Source

pub fn with_l1(self, backend: Arc<dyn CacheBackend>) -> Self

Configure a custom L1 (in-memory) cache backend

§Arguments
  • backend - Any type implementing CacheBackend trait
§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?;
Source

pub fn with_l2(self, backend: Arc<dyn L2CacheBackend>) -> Self

Configure a custom L2 (distributed) cache backend

§Arguments
  • backend - Any type implementing L2CacheBackend trait
§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?;
Source

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 implementing StreamingBackend trait
§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?;
Source

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 implementing L2CacheBackend trait
  • config - 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?;
Source

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?;
Source

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?;
Source

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 system
  • Err(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(())
}

Trait Implementations§

Source§

impl Default for CacheSystemBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more