use std::time::Duration;
pub mod cache2s;
pub mod cache3600s;
pub mod cache60s;
pub mod cache_bytes_1h;
pub use cache2s::*;
pub use cache3600s::*;
pub use cache60s::*;
pub use cache_bytes_1h::*;
use moka::sync::Cache;
pub struct YourCache<V> {
pub cache: Cache<String, V>,
}
impl<V> YourCache<V>
where
V: Clone + Send + Sync + 'static,
{
pub fn build(ttl_secs: usize, capacity: usize) -> Self {
Self {
cache: Cache::builder()
.time_to_live(Duration::from_secs(ttl_secs as u64))
.time_to_idle(Duration::from_secs(ttl_secs as u64))
.max_capacity(capacity as u64)
.build(),
}
}
pub fn put(&self, key: &str, value: V) {
self.cache.insert(key.to_string(), value);
}
pub fn put_if_absent(&self, key: &str, value: V) -> V {
if !self.exists(key) {
self.cache.insert(key.to_string(), value);
}
self.get(key).unwrap()
}
pub fn get(&self, key: &str) -> Option<V> {
self.cache.get(key)
}
pub fn exists(&self, key: &str) -> bool {
self.cache.contains_key(key)
}
}