pub struct Loader<K, V>{ /* private fields */ }
Expand description
Batch loads values from some expensive resource, primarily intended for mitigating GraphQL’s N+1 problem.
Users can call Loader::load
and Loader::load_many
to fetch values from the underlying resource or
cache. The cache can be cleared with calls to Loader::clear
and Loader::clear_many
, and values can be
added to the cache out-of-band through the use of Loader::prime
and Loader::prime_many
.
The Loader
struct acts as an intermediary between the async domain in which load
calls are
invoked and the pseudo-single-threaded domain of the LoaderWorker
. Callers can invoke the
Loader
from multiple parallel tasks, and the loader will enqueue the requested operations on
the request queue for processing by its LoaderWorker
. The worker processes the requests
sequentially and provides results via response oneshot channels back to the Loader.
Implementations§
Source§impl<K, V> Loader<K, V>
impl<K, V> Loader<K, V>
Source§impl<K, V> Loader<K, V>
impl<K, V> Loader<K, V>
Sourcepub async fn load(&self, key: K) -> Option<V>
pub async fn load(&self, key: K) -> Option<V>
Loads a value from the underlying resource.
Returns None if the value could not be loaded by the BatchFunction.
If the value is already in the loader cache, it is returned as soon as it is processed. Otherwise, the requested key is enqueued for batch loading in the next loader execution frame.
Sourcepub async fn load_many(&self, keys: Vec<K>) -> Vec<Option<V>>
pub async fn load_many(&self, keys: Vec<K>) -> Vec<Option<V>>
Loads many values at once.
Returns None for values that could not be loaded by the BatchFunction.
If all the values are already present in the laoder cache, they are returned as soon as the request is processed by the worker. Otherwise, the keys is enqueue for batch loading in the next loader execution frame.
Sourcepub async fn prime_many(&self, key_vals: Vec<(K, V)>)
pub async fn prime_many(&self, key_vals: Vec<(K, V)>)
Adds many values to the cache at once.
Sourcepub async fn clear(&self, key: K)
pub async fn clear(&self, key: K)
Removes a value from the cache.
This key will be reloaded when it is next requested.
Sourcepub async fn clear_many(&self, keys: Vec<K>)
pub async fn clear_many(&self, keys: Vec<K>)
Removes multiple values from the cache at once.
These keys will be reloaded when requested.