macro_rules! impl_cache {
(@mk_table $table_name:literal) => { ... };
(@lookup $db:expr, $key:expr) => { ... };
(@populate $db:expr, $key:expr, $value:expr) => { ... };
}Expand description
Implement caching.
ยงExample
use git_bug::impl_cache;
use git_bug::replica::Replica;
pub mod compute {
use git_bug::replica::cache;
#[derive(Debug)]
pub enum Error {
CacheError(cache::Error),
}
impl From<cache::Error> for Error {
fn from(value: cache::Error) -> Self {
Self::CacheError(value)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::CacheError(err) => err.fmt(f),
}
}
}
impl std::error::Error for Error {}
}
fn computation_heavy_function(replica: &Replica, id: String) -> Result<String, compute::Error> {
impl_cache!(@mk_table "operation_packs");
impl_cache! {@lookup replica.db(), id.as_bytes()}
let me = {
// Some very heavy computation.
String::new()
};
impl_cache! {@populate replica.db(), id.as_bytes(), &me}
Ok(me)
}You should provide the cache database ($db) (probably via
Replica::db) and a unique key ($key).