Struct dataload_rs::Loader [−][src]
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
impl<K, V> Loader<K, V> where
K: 'static + Eq + Debug + Ord + Copy + Hash + Send + Sync,
V: 'static + Send + Debug + Clone,
[src]
K: 'static + Eq + Debug + Ord + Copy + Hash + Send + Sync,
V: 'static + Send + Debug + Clone,
pub fn new<F, ContextT>(_: F, context: ContextT) -> Self where
ContextT: Send + Sync + 'static,
F: 'static + BatchFunction<K, V, Context = ContextT> + Send,
[src]
ContextT: Send + Sync + 'static,
F: 'static + BatchFunction<K, V, Context = ContextT> + Send,
Creates a new Loader for the provided BatchFunction and Context type.
Note: the batch function is passed in as a marker for type inference.
impl<K, V> Loader<K, V> where
K: 'static + Eq + Debug + Ord + Copy + Send + Sync,
V: 'static + Send + Debug + Clone,
[src]
K: 'static + Eq + Debug + Ord + Copy + Send + Sync,
V: 'static + Send + Debug + Clone,
pub async fn load(&self, key: K) -> Option<V>
[src]
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.
pub async fn load_many(&self, keys: Vec<K>) -> Vec<Option<V>>
[src]
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.
pub async fn prime(&self, key: K, value: V)
[src]
Adds a value to the cache.
pub async fn prime_many(&self, key_vals: Vec<(K, V)>)
[src]
Adds many values to the cache at once.
pub async fn clear(&self, key: K)
[src]
Removes a value from the cache.
This key will be reloaded when it is next requested.
pub async fn clear_many(&self, keys: Vec<K>)
[src]
Removes multiple values from the cache at once.
These keys will be reloaded when requested.
Trait Implementations
impl<K, V> Drop for Loader<K, V> where
K: 'static + Eq + Debug + Copy + Send,
V: 'static + Send + Debug + Clone,
[src]
K: 'static + Eq + Debug + Copy + Send,
V: 'static + Send + Debug + Clone,
Auto Trait Implementations
impl<K, V> !RefUnwindSafe for Loader<K, V>
impl<K, V> Send for Loader<K, V>
impl<K, V> Sync for Loader<K, V>
impl<K, V> Unpin for Loader<K, V>
impl<K, V> !UnwindSafe for Loader<K, V>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> WithSubscriber for T
[src]
pub fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
[src]
S: Into<Dispatch>,