Trait ultra_batch::Fetcher
source · [−]pub trait Fetcher {
type Key: Clone + Hash + Eq + Send + Sync;
type Value: Clone + Send + Sync;
type Error: Display + Send + Sync + 'static;
fn fetch<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
keys: &'life1 [Self::Key],
values: &'life2 mut Cache<'life3, Self::Key, Self::Value>
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>>
where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait;
}
Expand description
A trait for fetching values from some datastore in bulk. A Fetcher
will be given an array of keys and should insert fetched values into
a given cache. Implementing Fetcher
will allow queries to be batch
using a Batcher
. See the Batcher
docs for details about batching, caching, and error semantics.
Implementors should use async-trait
to implement this trait.
Examples
struct UserFetcher {
db_conn: DbConnection,
}
#[async_trait]
impl Fetcher for UserFetcher {
type Key = UserId;
type Value = User;
type Error = anyhow::Error;
async fn fetch(&self, keys: &[UserId], values: &mut Cache<'_, UserId, User>) -> anyhow::Result<()> {
let users = self.db_conn.get_users_by_ids(keys).await?;
for user in users {
values.insert(user.id, user);
}
Ok(())
}
}
Associated Types
The type returned in a batch. Value
is usually a single database
record, but could also be a more sophisticated type, such as a
Vec
of values for a Fetcher
that deals with one-to-many
relationships.
Required methods
fn fetch<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
keys: &'life1 [Self::Key],
values: &'life2 mut Cache<'life3, Self::Key, Self::Value>
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
fn fetch<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
keys: &'life1 [Self::Key],
values: &'life2 mut Cache<'life3, Self::Key, Self::Value>
) -> Pin<Box<dyn Future<Output = Result<(), Self::Error>> + Send + 'async_trait>> where
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: 'async_trait,
Retrieve the values associated with the given keys, and insert them into
values
if found. If Ok(_)
is returned, then any keys not inserted
into values
will be marked as “not found” (meaning any future attempts
to retrieve them will fail). If Err(_)
is returned, then the caller(s)
waiting on the batch will receive a LoadError::FetchError
with the message from returned error (note that any values inserted into
values
before the Err(_)
is returned will still be cached). See the
Batcher
docs for more details.