Struct Loader

Source
pub struct Loader<K, V>
where K: 'static + Eq + Debug + Copy + Send, V: 'static + Send + Debug + Clone,
{ /* 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>
where K: 'static + Eq + Debug + Ord + Copy + Hash + Send + Sync, V: 'static + Send + Debug + Clone,

Source

pub fn new<F, ContextT>(_: F, context: ContextT) -> Self
where 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.

Source§

impl<K, V> Loader<K, V>
where K: 'static + Eq + Debug + Ord + Copy + Send + Sync, V: 'static + Send + Debug + Clone,

Source

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.

Source

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.

Source

pub async fn prime(&self, key: K, value: V)

Adds a value to the cache.

Source

pub async fn prime_many(&self, key_vals: Vec<(K, V)>)

Adds many values to the cache at once.

Source

pub async fn clear(&self, key: K)

Removes a value from the cache.

This key will be reloaded when it is next requested.

Source

pub async fn clear_many(&self, keys: Vec<K>)

Removes multiple values from the cache at once.

These keys will be reloaded when requested.

Trait Implementations§

Source§

impl<K, V> Drop for Loader<K, V>
where K: 'static + Eq + Debug + Copy + Send, V: 'static + Send + Debug + Clone,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for Loader<K, V>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.