Skip to main content

Compose

Trait Compose 

Source
pub trait Compose: Backend + Sized {
    // Provided methods
    fn compose<L2, O>(
        self,
        l2: L2,
        offload: O,
    ) -> CompositionBackend<Self, L2, O>
       where L2: Backend,
             O: Offload<'static> { ... }
    fn compose_with<L2, O, R, W>(
        self,
        l2: L2,
        offload: O,
        policy: CompositionPolicy<R, W>,
    ) -> CompositionBackend<Self, L2, O, R, W>
       where L2: Backend,
             O: Offload<'static>,
             R: CompositionReadPolicy,
             W: CompositionWritePolicy { ... }
}
Expand description

Trait for composing backends into layered cache hierarchies.

This trait is automatically implemented for all types that implement Backend, providing a fluent API for creating CompositionBackend instances.

§Examples

use hitbox_backend::composition::Compose;

// Simple composition with default policies
let cache = l1_backend.compose(l2_backend, offload);

// Composition with custom policies
let policy = CompositionPolicy::new()
    .read(RaceReadPolicy::new())
    .write(SequentialWritePolicy::new());

let cache = l1_backend.compose_with(l2_backend, offload, policy);

Provided Methods§

Source

fn compose<L2, O>(self, l2: L2, offload: O) -> CompositionBackend<Self, L2, O>
where L2: Backend, O: Offload<'static>,

Compose this backend with another backend as L2, using default policies.

This creates a CompositionBackend where:

  • self becomes L1 (first layer, checked first on reads)
  • l2 becomes L2 (second layer, checked if L1 misses)

Default policies:

  • Read: SequentialReadPolicy (try L1 first, then L2)
  • Write: OptimisticParallelWritePolicy (write to both, succeed if ≥1 succeeds)
  • Refill: AlwaysRefill (always populate L1 after L2 hit)
§Arguments
  • l2 - The second-layer backend
  • offload - Offload manager for background tasks
§Example
use hitbox_backend::composition::Compose;
use hitbox_moka::MokaBackend;
use hitbox_redis::{RedisBackend, ConnectionMode};

let moka = MokaBackend::builder().max_entries(1000).build();
let redis = RedisBackend::builder()
    .connection(ConnectionMode::single("redis://localhost/"))
    .build()?;

// Moka as L1, Redis as L2
let cache = moka.compose(redis, offload);
Source

fn compose_with<L2, O, R, W>( self, l2: L2, offload: O, policy: CompositionPolicy<R, W>, ) -> CompositionBackend<Self, L2, O, R, W>

Compose this backend with another backend as L2, using custom policies.

This provides full control over read, write, and refill policies.

§Arguments
  • l2 - The second-layer backend
  • offload - Offload manager for background tasks
  • policy - Custom composition policies
§Example
use hitbox_backend::composition::{Compose, CompositionPolicy};
use hitbox_backend::composition::policy::{RaceReadPolicy, SequentialWritePolicy};

let policy = CompositionPolicy::new()
    .read(RaceReadPolicy::new())
    .write(SequentialWritePolicy::new());

let cache = l1.compose_with(l2, offload, policy);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Backend> Compose for T