pub struct BatchResolver<T: Clone + Send> { /* private fields */ }Expand description
A DataLoader that batches and caches item lookups.
Items are queued individually via queue(), then fetched all at once
with resolve_all(). Subsequent lookups of cached keys skip the loader.
Implementations§
Source§impl<T: Clone + Send + 'static> BatchResolver<T>
impl<T: Clone + Send + 'static> BatchResolver<T>
Sourcepub fn new(
loader: Arc<dyn BatchLoader<Item = T>>,
max_batch_size: usize,
) -> Self
pub fn new( loader: Arc<dyn BatchLoader<Item = T>>, max_batch_size: usize, ) -> Self
Create a new BatchResolver with the given loader and maximum batch size.
max_batch_size controls how many keys are sent per load_batch call.
A value of 0 is treated as unlimited (all queued keys in one batch).
Sourcepub fn queue(&mut self, key: impl Into<String>)
pub fn queue(&mut self, key: impl Into<String>)
Add a key to the pending queue.
Duplicates are ignored: a key already in the cache or already queued will not be queued again.
Sourcepub fn resolve_all(&mut self) -> Result<(), BatchError>
pub fn resolve_all(&mut self) -> Result<(), BatchError>
Execute all pending batches, populating the internal cache.
Keys are split into batches of at most max_batch_size and passed to
the loader. After this call pending_count() returns 0.
Sourcepub fn get(&self, key: &str) -> Option<&T>
pub fn get(&self, key: &str) -> Option<&T>
Retrieve a cached item by key.
Returns None if the key was never resolved or was missing from the loader.
Sourcepub fn cache_size(&self) -> usize
pub fn cache_size(&self) -> usize
Number of items in the cache.
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Clear the internal cache (does not clear the queue).
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Number of keys currently pending resolution.
Sourcepub fn batches_executed(&self) -> usize
pub fn batches_executed(&self) -> usize
Total number of load_batch calls made so far.
Auto Trait Implementations§
impl<T> !RefUnwindSafe for BatchResolver<T>
impl<T> !UnwindSafe for BatchResolver<T>
impl<T> Freeze for BatchResolver<T>
impl<T> Send for BatchResolver<T>
impl<T> Sync for BatchResolver<T>
impl<T> Unpin for BatchResolver<T>
impl<T> UnsafeUnpin for BatchResolver<T>
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more