Skip to main content

Cache

Struct Cache 

Source
pub struct Cache<V: Clone + Send + Sync + 'static> { /* private fields */ }
Expand description

A robust, multi-level cache.

Cache<V> is generic over a single value type V. Unlike FusionCache (which leans on .NET runtime type information to store heterogeneous values in one instance), the idiomatic Rust model is one value type per cache — this makes “wrong type for this key” unrepresentable instead of a run-time downcast. Use several caches, or a sum type / serde_json::Value, for heterogeneous values.

Cloning a Cache is cheap (it shares one underlying instance) and is how you hand it to other tasks.

Implementations§

Source§

impl<V: Clone + Send + Sync + 'static> Cache<V>

Source

pub fn builder() -> CacheBuilder<V>

Starts building a cache.

Source

pub fn new() -> Self

Creates a cache with all default settings and the real system clock.

Source

pub fn name(&self) -> &str

The cache’s name.

Source

pub fn events(&self) -> &Events

The event hub; subscribe to observe hits, misses, fail-safe activations…

Source

pub fn entry_options(&self) -> EntryOptions

A clone of the cache’s default entry options, ready to tweak per call.

Source

pub fn wait_for_initial_backplane_subscribe(&self) -> bool

Whether the cache establishes its backplane subscription before build returns (FusionCache WaitForInitialBackplaneSubscribe). In amalgam this is inherently satisfied — see CacheBuilder::wait_for_initial_backplane_subscribe.

Source

pub async fn get_or_set<F, Fut>( &self, key: impl AsRef<str>, factory: F, ) -> Result<V>
where F: FnOnce(FactoryContext<V>) -> Fut + Send + 'static, Fut: Future<Output = Result<FactoryProduct<V>, FactoryError>> + Send + 'static,

Returns the cached value for key, or runs factory to produce it.

Uses the cache’s default options, no tags and no fail-safe default. See get_or_set_full for the complete form.

Source

pub async fn get_or_set_with<F, Fut>( &self, key: impl AsRef<str>, factory: F, options: EntryOptions, ) -> Result<V>
where F: FnOnce(FactoryContext<V>) -> Fut + Send + 'static, Fut: Future<Output = Result<FactoryProduct<V>, FactoryError>> + Send + 'static,

Like get_or_set but with explicit per-call options.

Source

pub async fn get_or_set_value( &self, key: impl AsRef<str>, value: V, options: Option<EntryOptions>, ) -> Result<V>

Returns the cached value for key, or stores and returns the constant value if it is absent.

The constant-value form of get_or_set: it goes through the same L1 → L2 → single-flight flow, but the “factory” simply yields value.

Source

pub async fn get_or_set_full<F, Fut>( &self, key: impl AsRef<str>, factory: F, options: Option<EntryOptions>, tags: Box<[Tag]>, fail_safe_default: MaybeValue<V>, ) -> Result<V>
where F: FnOnce(FactoryContext<V>) -> Fut + Send + 'static, Fut: Future<Output = Result<FactoryProduct<V>, FactoryError>> + Send + 'static,

The full get_or_set: per-call options, tags for the produced entry, and a fail_safe_default served as a last resort when the factory fails and no stale value exists.

Source

pub async fn set(&self, key: impl AsRef<str>, value: V)

Writes value with the cache’s default options and no tags.

Source

pub async fn set_full( &self, key: impl AsRef<str>, value: V, options: Option<EntryOptions>, tags: Box<[Tag]>, )

Writes value with explicit options and tags.

Source

pub async fn try_get( &self, key: impl AsRef<str>, options: Option<EntryOptions>, ) -> MaybeValue<V>

Reads a value without ever running a factory. A miss (or a stale entry, unless allow_stale_on_read_only is set) yields MaybeValue::none.

Source

pub async fn get_or_default( &self, key: impl AsRef<str>, default: V, options: Option<EntryOptions>, ) -> V

Reads a value or returns default (never runs a factory).

Source

pub async fn remove(&self, key: impl AsRef<str>)

Removes an entry from L1 and L2 and tells peers to evict it.

Source

pub async fn expire(&self, key: impl AsRef<str>)

Logically expires an entry: it is no longer fresh, but fail-safe can still serve it as a stale fallback. Propagated to L2 and peers.

Source

pub async fn remove_by_tag(&self, tag: impl AsRef<str>)

Invalidates every entry carrying tag (lazily — entries are dropped on their next read). No-op for a blank tag.

Source

pub async fn remove_by_tags<I, S>(&self, tags: I)
where I: IntoIterator<Item = S>, S: AsRef<str>,

Invalidates every entry carrying any of tags.

Source

pub async fn clear(&self, allow_fail_safe: bool)

Clears the whole cache.

With allow_fail_safe = true (the FusionCache default) every entry is logically expired so fail-safe can still serve stale values; with false they are hard-removed.

Source

pub async fn run_pending_tasks(&self)

Runs the L1 backend’s pending maintenance (eviction, expiry). Primarily for deterministic tests.

Trait Implementations§

Source§

impl<V: Clone + Send + Sync + 'static> Clone for Cache<V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<V: Clone + Send + Sync + 'static> Default for Cache<V>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<V> !RefUnwindSafe for Cache<V>

§

impl<V> !UnwindSafe for Cache<V>

§

impl<V> Freeze for Cache<V>
where Arc<CacheInner<V>>: Freeze,

§

impl<V> Send for Cache<V>
where Arc<CacheInner<V>>: Send,

§

impl<V> Sync for Cache<V>
where Arc<CacheInner<V>>: Sync,

§

impl<V> Unpin for Cache<V>
where Arc<CacheInner<V>>: Unpin,

§

impl<V> UnsafeUnpin for Cache<V>
where Arc<CacheInner<V>>: UnsafeUnpin,

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> 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> 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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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