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: PrefetchStrategyStrategy driving key prediction.
Implementations§
Source§impl Prefetcher
impl Prefetcher
Sourcepub fn new(strategy: PrefetchStrategy, cache: Arc<dyn Cache>) -> Self
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.
Sourcepub fn with_loader<F>(
strategy: PrefetchStrategy,
cache: Arc<dyn Cache>,
loader: F,
) -> Self
pub fn with_loader<F>( strategy: PrefetchStrategy, cache: Arc<dyn Cache>, loader: F, ) -> Self
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).
Sourcepub fn with_max_pending(self, max: usize) -> Self
pub fn with_max_pending(self, max: usize) -> Self
Set the maximum number of pending prefetch requests.
Sourcepub fn trigger_prefetch(&self, current_key: &str)
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.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Return the number of keys currently in the pending queue.
Sourcepub fn drain_pending(&self) -> Vec<String>
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.