Skip to main content

CompositionBackend

Struct CompositionBackend 

Source
pub struct CompositionBackend<L1, L2, O, R = SequentialReadPolicy, W = OptimisticParallelWritePolicy>{ /* private fields */ }
Expand description

A backend that composes two cache backends into a layered caching system.

The first backend (L1) is checked first on reads, and if not found, the second backend (L2) is checked. On writes, both backends are updated.

Each layer can use its own serialization format and compression since CacheBackend operates on typed data, not raw bytes.

Behavior can be customized via CompositionReadPolicy, CompositionWritePolicy, and RefillPolicy to control how reads, writes, and L1 refills are executed across the layers.

Implementations§

Source§

impl<L1, L2, O> CompositionBackend<L1, L2, O, SequentialReadPolicy, OptimisticParallelWritePolicy>
where L1: Backend, L2: Backend, O: Offload<'static>,

Source

pub fn new(l1: L1, l2: L2, offload: O) -> Self

Creates a new composition backend with two layers using default policies.

Default policies:

  • Read: SequentialReadPolicy (try L1 first, then L2)
  • Write: OptimisticParallelWritePolicy (write to both, succeed if ≥1 succeeds)
  • Refill: RefillPolicy::Never (do not populate L1 after L2 hit)
§Arguments
  • l1 - First-layer backend (checked first on reads)
  • l2 - Second-layer backend (checked if L1 misses)
  • offload - Offload manager for background tasks (e.g., race policy losers)
Source§

impl<L1, L2, O, R, W> CompositionBackend<L1, L2, O, R, W>

Source

pub fn read_policy(&self) -> &R

Returns a reference to the read policy.

Source

pub fn write_policy(&self) -> &W

Returns a reference to the write policy.

Source

pub fn refill_policy(&self) -> &RefillPolicy

Returns a reference to the refill policy.

Source

pub fn offload(&self) -> &O

Returns a reference to the offload manager.

Source

pub fn label(self, label: impl Into<BackendLabel>) -> Self

Set a custom label for this backend.

The label is used for source path composition in multi-layer caches. For example, with label “cache”, the source path might be “cache.L1”.

Source

pub fn with_policy<NewR, NewW>( self, policy: CompositionPolicy<NewR, NewW>, ) -> CompositionBackend<L1, L2, O, NewR, NewW>

Set all policies at once using CompositionPolicy builder.

This is the preferred way to configure multiple policies.

§Example
use hitbox_backend::{CompositionBackend, composition::CompositionPolicy};
use hitbox_backend::composition::policy::{RaceReadPolicy, SequentialWritePolicy, RefillPolicy};

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

let backend = CompositionBackend::new(l1, l2, offload)
    .with_policy(policy);
Source

pub fn read<NewR: CompositionReadPolicy>( self, read_policy: NewR, ) -> CompositionBackend<L1, L2, O, NewR, W>

Set the read policy (builder pattern).

This consumes the backend and returns a new one with the updated read policy.

§Example
use hitbox_backend::CompositionBackend;
use hitbox_backend::composition::policy::RaceReadPolicy;

let backend = CompositionBackend::new(l1, l2, offload)
    .read(RaceReadPolicy::new());
Source

pub fn write<NewW: CompositionWritePolicy>( self, write_policy: NewW, ) -> CompositionBackend<L1, L2, O, R, NewW>

Set the write policy (builder pattern).

This consumes the backend and returns a new one with the updated write policy.

§Example
use hitbox_backend::CompositionBackend;
use hitbox_backend::composition::policy::SequentialWritePolicy;

let backend = CompositionBackend::new(l1, l2, offload)
    .write(SequentialWritePolicy::new());
Source

pub fn refill(self, refill_policy: RefillPolicy) -> Self

Set the refill policy (builder pattern).

This consumes the backend and returns a new one with the updated refill policy.

§Example
use hitbox_backend::CompositionBackend;
use hitbox_backend::composition::policy::RefillPolicy;

let backend = CompositionBackend::new(l1, l2, offload)
    .refill(RefillPolicy::Always);

Trait Implementations§

Source§

impl<L1, L2, O, R, W> Backend for CompositionBackend<L1, L2, O, R, W>
where L1: Backend + Clone + Send + Sync + 'static, L2: Backend + Clone + Send + Sync + 'static, O: Offload<'static>, R: CompositionReadPolicy, W: CompositionWritePolicy,

Source§

fn read<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CacheKey, ) -> Pin<Box<dyn Future<Output = BackendResult<Option<CacheValue<Raw>>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Read raw cached data by key. Read more
Source§

fn write<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CacheKey, value: CacheValue<Raw>, ) -> Pin<Box<dyn Future<Output = BackendResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Write raw data to cache.
Source§

fn remove<'life0, 'life1, 'async_trait>( &'life0 self, key: &'life1 CacheKey, ) -> Pin<Box<dyn Future<Output = BackendResult<DeleteStatus>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Remove data from cache.
Source§

fn label(&self) -> BackendLabel

Backend label for metrics and source path composition. Read more
Source§

fn value_format(&self) -> &dyn Format

Serialization format for cached values. Default: BincodeFormat.
Source§

fn key_format(&self) -> &CacheKeyFormat

Key serialization format. Default: CacheKeyFormat::Bitcode.
Source§

fn compressor(&self) -> &dyn Compressor

Compressor for cached values. Default: PassthroughCompressor.
Source§

impl<L1, L2, O, R, W> CacheBackend for CompositionBackend<L1, L2, O, R, W>
where L1: CacheBackend + Clone + Send + Sync + 'static, L2: CacheBackend + Clone + Send + Sync + 'static, O: Offload<'static>, R: CompositionReadPolicy, W: CompositionWritePolicy,

Source§

async fn get<T>( &self, key: &CacheKey, ctx: &mut BoxContext, ) -> BackendResult<Option<CacheValue<T::Cached>>>

Retrieve a typed value from cache. Read more
Source§

async fn set<T>( &self, key: &CacheKey, value: &CacheValue<T::Cached>, ctx: &mut BoxContext, ) -> BackendResult<()>

Store a typed value in cache. Read more
Source§

async fn delete( &self, key: &CacheKey, ctx: &mut BoxContext, ) -> BackendResult<DeleteStatus>

Delete a value from cache. Read more
Source§

impl<L1, L2, O, R, W> Clone for CompositionBackend<L1, L2, O, R, W>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<L1, L2, O, R, W> Debug for CompositionBackend<L1, L2, O, R, W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<L1, L2, O, R, W> Freeze for CompositionBackend<L1, L2, O, R, W>
where L1: Freeze, L2: Freeze, O: Freeze, R: Freeze, W: Freeze,

§

impl<L1, L2, O, R = SequentialReadPolicy, W = OptimisticParallelWritePolicy> !RefUnwindSafe for CompositionBackend<L1, L2, O, R, W>

§

impl<L1, L2, O, R, W> Send for CompositionBackend<L1, L2, O, R, W>

§

impl<L1, L2, O, R, W> Sync for CompositionBackend<L1, L2, O, R, W>

§

impl<L1, L2, O, R, W> Unpin for CompositionBackend<L1, L2, O, R, W>
where L1: Unpin, L2: Unpin, O: Unpin, R: Unpin, W: Unpin,

§

impl<L1, L2, O, R = SequentialReadPolicy, W = OptimisticParallelWritePolicy> !UnwindSafe for CompositionBackend<L1, L2, O, R, W>

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

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> CloneToUninit for T
where T: Clone,

Source§

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
Source§

impl<T> Compose for T
where T: Backend,

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. Read more
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. 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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

Uses borrowed data to replace owned data, usually by cloning. 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