use super::*;
struct TTLSet {
cache: moka::sync::Cache<String, ()>,
}
impl TTLSet {
pub fn new(ttl: Duration) -> Self {
let builder = moka::sync::Cache::builder()
.initial_capacity(1000)
.time_to_live(ttl);
Self {
cache: builder.build(),
}
}
pub fn exists(&self, k: &str) -> bool {
self.cache.contains_key(k)
}
pub fn insert(&self, k: String) {
self.cache.insert(k, ());
}
}
pub struct ResponseCache {
responses: HashMap<String, Bytes>,
completes: TTLSet,
}
impl ResponseCache {
pub fn new() -> Self {
Self {
responses: HashMap::new(),
completes: TTLSet::new(Duration::from_secs(5)),
}
}
pub fn should_execute(&self, k: &str) -> bool {
if self.completes.exists(k) {
return false;
}
match self.responses.get(k) {
Some(_) => false,
None => true,
}
}
pub fn insert_response(&mut self, k: String, v: Bytes) {
self.responses.insert(k, v);
}
pub fn get_response(&self, k: &str) -> Option<Bytes> {
self.responses.get(k).cloned()
}
pub fn complete_response(&mut self, k: &str) {
self.responses.remove(k);
self.completes.insert(k.to_owned());
}
}