CacheSystem

Struct CacheSystem 

Source
pub struct CacheSystem {
    pub cache_manager: Arc<CacheManager>,
    pub l1_cache: Option<Arc<L1Cache>>,
    pub l2_cache: Option<Arc<L2Cache>>,
}
Expand description

Main entry point for the Multi-Tier Cache system

Provides unified access to L1 (Moka) and L2 (Redis) caches with automatic failover, promotion, and stampede protection.

§Example

use multi_tier_cache::CacheSystem;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cache = CacheSystem::new().await?;

    // Use cache_manager for all operations
    let manager = cache.cache_manager();

    Ok(())
}

§Note on l1_cache and l2_cache Fields

When using multi-tier mode or custom backends, l1_cache and l2_cache may be None. Always use cache_manager() for cache operations.

Fields§

§cache_manager: Arc<CacheManager>

Unified cache manager (primary interface)

§l1_cache: Option<Arc<L1Cache>>

L1 Cache (in-memory, Moka) - None when using custom backends

§l2_cache: Option<Arc<L2Cache>>

L2 Cache (distributed, Redis) - None when using custom backends

Implementations§

Source§

impl CacheSystem

Source

pub async fn new() -> Result<Self>

Create new cache system with default configuration

§Configuration

Redis connection is configured via REDIS_URL environment variable. Default: redis://127.0.0.1:6379

§Example
use multi_tier_cache::CacheSystem;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Set environment variable (optional)
    std::env::set_var("REDIS_URL", "redis://localhost:6379");

    let cache = CacheSystem::new().await?;
    Ok(())
}
§Errors

Returns an error if cache initialization fails.

Source

pub async fn with_redis_url(redis_url: &str) -> Result<Self>

Create cache system with custom Redis URL

§Arguments
  • redis_url - Redis connection string (e.g., <redis://localhost:6379>)
§Example
use multi_tier_cache::CacheSystem;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cache = CacheSystem::with_redis_url("redis://custom:6379").await?;
    Ok(())
}
§Errors

Returns an error if cache initialization fails.

Source

pub async fn health_check(&self) -> bool

Perform health check on all cache tiers

Returns true if at least L1 is operational. L2 failure is tolerated (graceful degradation).

§Example
use multi_tier_cache::CacheSystem;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let cache = CacheSystem::new().await?;

    if cache.health_check().await {
        tracing::info!("Cache system healthy");
    }

    Ok(())
}
Source

pub fn cache_manager(&self) -> &Arc<CacheManager>

Get reference to cache manager (primary interface)

Use this for all cache operations: get, set, streams, etc.

Trait Implementations§

Source§

impl Clone for CacheSystem

Source§

fn clone(&self) -> CacheSystem

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

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
§

impl<T, U> Into<U> for T
where U: From<T>,

§

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
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

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