Skip to main content

CachePreloader

Trait CachePreloader 

Source
pub trait CachePreloader:
    Send
    + Sync
    + 'static {
    // Required methods
    fn predict(&self, accessed_key: &str) -> Vec<(String, u32)>;
    fn warm(&self, key: &str);
}
Expand description

Hook for predictive cache preloading.

ThingsCache calls CachePreloader::predict after every get_* access (hit or miss) to ask “given that key X was just accessed, what should we queue for background warming?” The returned (key, priority) pairs are pushed into the cache’s priority queue via [ThingsCache::add_to_warming].

On each warming-loop tick, the cache picks the top-priority queued keys and calls CachePreloader::warm for each. The implementor is expected to fetch the data and populate the cache (typically by tokio::spawning a task that calls back into cache.get_*(key, fetcher)). warm is fire-and-forget — errors must be handled internally.

The trait is synchronous to stay dyn-compatible without async-trait. Implementors that need async work should spawn it inside warm.

Required Methods§

Source

fn predict(&self, accessed_key: &str) -> Vec<(String, u32)>

Called after a cache access. Returns (key, priority) pairs to enqueue for background warming. Return vec![] to opt out for this access.

Source

fn warm(&self, key: &str)

Called by the warming loop for each top-priority queued key. Implementor fetches and populates the cache, typically via tokio::spawn.

Implementors§