Skip to main content

Prefetcher

Struct Prefetcher 

Source
pub struct Prefetcher {
    pub strategy: PrefetchStrategy,
    /* private fields */
}
Expand description

A prefetcher that warms a Cache based on the configured PrefetchStrategy.

Call trigger_prefetch each time a key is accessed; the prefetcher will synchronously insert placeholder entries for predicted future keys that are not already present.

§Thread safety

Prefetcher is Clone and thread-safe when the inner cache is Send + Sync. The pending queue is protected by a Mutex.

Fields§

§strategy: PrefetchStrategy

Strategy driving key prediction.

Implementations§

Source§

impl Prefetcher

Source

pub fn new(strategy: PrefetchStrategy, cache: Arc<dyn Cache>) -> Self

Create a new Prefetcher with the given strategy and cache.

Uses a no-op loader that inserts empty-byte placeholder entries.

Source

pub fn with_loader<F>( strategy: PrefetchStrategy, cache: Arc<dyn Cache>, loader: F, ) -> Self
where F: Fn(&str) -> Vec<u8> + Send + Sync + 'static,

Create a Prefetcher with a custom value loader function.

The loader receives the key and returns the bytes that should be stored in the cache for that key (e.g. reads from disk or network).

Source

pub fn with_max_pending(self, max: usize) -> Self

Set the maximum number of pending prefetch requests.

Source

pub fn trigger_prefetch(&self, current_key: &str)

Trigger a prefetch based on current_key.

Predicts the next keys using the configured strategy and immediately inserts them into the cache via the loader if they are not already present. Predictions that cannot be determined (e.g. key does not match the expected pattern) are silently ignored.

Source

pub fn pending_count(&self) -> usize

Return the number of keys currently in the pending queue.

Source

pub fn drain_pending(&self) -> Vec<String>

Drain the pending queue and return all queued keys.

This can be used by a background worker to post-process prefetch completions.

Source

pub fn cache(&self) -> &Arc<dyn Cache>

Return a reference to the underlying cache.

Trait Implementations§

Source§

impl Debug for Prefetcher

Source§

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

Formats the value using the given formatter. 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, 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.