zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
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 (TTL): 1 minutes
                .time_to_live(Duration::from_secs(ttl_secs as u64))
                // Time to idle (TTI):  1 minutes
                .time_to_idle(Duration::from_secs(ttl_secs as u64))
                // This cache will hold up to 5MiB of values.
                .max_capacity(capacity as u64)
                // Create the cache.
                .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)
    }
}