mod entry;
mod key;
mod lookup;
mod weight;
pub(crate) use entry::Entry;
pub(crate) use lookup::Lookup;
use quick_cache::sync::DefaultLifecycle;
use quick_cache::{DefaultHashBuilder, OptionsBuilder};
pub(crate) type Cache = quick_cache::sync::Cache<key::Key, Entry, weight::Weight>;
pub struct TransactionCache {
cache: Cache,
}
impl TransactionCache {
pub(in crate::kvs) fn new(size: usize) -> Self {
let options = OptionsBuilder::new()
.estimated_items_capacity(size)
.weight_capacity(size as u64)
.shards(1)
.build()
.expect("valid transaction cache options");
let cache = Cache::with_options(
options,
weight::Weight,
DefaultHashBuilder::default(),
DefaultLifecycle::default(),
);
Self {
cache,
}
}
pub(crate) fn get(&self, lookup: &Lookup) -> Option<Entry> {
self.cache.get(lookup)
}
pub(crate) fn insert(&self, lookup: Lookup, entry: Entry) {
self.cache.insert(lookup.into(), entry);
}
pub(crate) fn remove(&self, lookup: &Lookup<'_>) {
self.cache.remove(lookup);
}
pub(crate) fn clear(&self) {
self.cache.clear();
}
}