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§
Required Methods§
Sourcefn get_if_present(&self, k: Self::K) -> Option<Self::V>
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.
Sourcefn 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 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.
Sourcefn 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 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.
Sourcefn invalidate(&self, k: Self::K)
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§
Sourcefn 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,
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.