use crate::cache::DEKCacheTrait;
use crate::error::cache::CacheError;
use mini_moka::sync::Cache;
use std::time::Duration;
pub struct MokaCache {
pub(crate) cache: Cache<String, Vec<u8>>,
}
impl MokaCache {
pub fn new(capacity: u64, ttl: Duration, tti: Duration) -> Self {
let cache = Cache::builder()
.max_capacity(capacity)
.time_to_live(ttl)
.time_to_idle(tti)
.build();
MokaCache { cache }
}
}
impl DEKCacheTrait for MokaCache {
type Identifier = String;
fn get(&self, k: &String) -> Option<Vec<u8>> {
self.cache.get(&hex::encode(k))
}
fn set(&self, k: String, v: Vec<u8>) -> Result<Vec<u8>, CacheError> {
self.cache.insert(hex::encode(k), v.clone());
Ok(v)
}
}