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.

§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)
  • 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

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

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

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, 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.