LoadingCache

Trait LoadingCache 

Source
pub trait LoadingCache:
    Debug
    + Send
    + Sync
    + 'static {
    type K: Clone + Eq + Hash + Debug + Ord + Send + 'static;
    type V: Clone + Debug + Send + 'static;
    type GetExtra: Debug + Send + 'static;

    // Required methods
    fn get_if_present(&self, k: Self::K) -> Option<Self::V>;
    fn get_with_status<'life0, 'async_trait>(
        &'life0 self,
        k: Self::K,
        extra: Self::GetExtra,
    ) -> Pin<Box<dyn Future<Output = (Self::V, CacheGetStatus)> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn put<'life0, 'async_trait>(
        &'life0 self,
        k: Self::K,
        v: Self::V,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn invalidate(&self, k: Self::K);

    // Provided method
    fn get<'life0, 'async_trait>(
        &'life0 self,
        k: Self::K,
        extra: Self::GetExtra,
    ) -> Pin<Box<dyn Future<Output = Self::V> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

High-level loading cache interface.

Cache entries are manually added using get(Key, GetExtra) or put(Key, Value), and are stored in the cache until either evicted or manually invalidated.

§Concurrency

Multiple cache requests for different keys can run at the same time. When data is requested for the same key, the underlying loader will only be polled once, even when the requests are made while the loader is still running.

§Cancellation

Canceling a get request will NOT cancel the underlying loader. The data will still be cached.

Required Associated Types§

Source

type K: Clone + Eq + Hash + Debug + Ord + Send + 'static

Cache key.

Source

type V: Clone + Debug + Send + 'static

Cache value.

Source

type GetExtra: Debug + Send + 'static

Extra data that is provided during GET but that is NOT part of the cache key.

Required Methods§

Source

fn get_if_present(&self, k: Self::K) -> Option<Self::V>

Get value from cache.

In contrast to get this will only return a value if there is a stored value. This will NOT start a new loading task.

Source

fn get_with_status<'life0, 'async_trait>( &'life0 self, k: Self::K, extra: Self::GetExtra, ) -> Pin<Box<dyn Future<Output = (Self::V, CacheGetStatus)> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get value from cache and the status.

Note that extra is only used if the key is missing from the storage backend and no value loader for this key is running yet.

Source

fn put<'life0, 'async_trait>( &'life0 self, k: Self::K, v: Self::V, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Side-load an entry into the cache. If the cache previously contained a value associated with key, the old value is replaced by value.

This will also complete a currently running loader for this key.

Source

fn invalidate(&self, k: Self::K)

Discard any cached value for the key.

This will also interrupt a currently running loader for this key.

Provided Methods§

Source

fn get<'life0, 'async_trait>( &'life0 self, k: Self::K, extra: Self::GetExtra, ) -> Pin<Box<dyn Future<Output = Self::V> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Get value from cache.

Note that extra is only used if the key is missing from the storage backend and no value loader for this key is running yet.

Implementors§

Source§

impl<K, V, L> LoadingCache for CacheDriver<K, V, L>
where K: Clone + Eq + Hash + Ord + Debug + Send + 'static, V: Clone + Debug + Send + 'static, L: CacheLoader<K = K, V = V>,

Source§

type K = K

Source§

type V = V

Source§

type GetExtra = <L as CacheLoader>::Extra

Source§

impl<L> LoadingCache for LoadingCacheWithListener<L>
where L: LoadingCache,