cachekit-rs 0.7.0

Caching for Rust (beta). Supports cachekit.io SaaS, Redis, Memcached, local File, and Cloudflare Workers.
Documentation
#[cfg(feature = "l1")]
mod tests {
    use cachekit::l1::L1Cache;
    use std::time::Duration;

    #[test]
    fn l1_set_and_get() {
        let cache = L1Cache::new(16);
        cache.set("key1", b"hello", Duration::from_secs(60));
        let result = cache.get("key1");
        assert_eq!(result, Some(b"hello".to_vec()));
    }

    #[test]
    fn l1_get_missing() {
        let cache = L1Cache::new(16);
        let result = cache.get("nonexistent");
        assert_eq!(result, None);
    }

    #[test]
    fn l1_delete() {
        let cache = L1Cache::new(16);
        cache.set("del_key", b"value", Duration::from_secs(60));
        assert!(cache.get("del_key").is_some());
        cache.delete("del_key");
        cache.run_pending_tasks();
        assert_eq!(cache.get("del_key"), None);
    }

    #[test]
    fn l1_expired_entry() {
        let cache = L1Cache::new(16);
        cache.set("exp_key", b"data", Duration::from_millis(1));
        std::thread::sleep(Duration::from_millis(50));
        cache.run_pending_tasks();
        assert_eq!(cache.get("exp_key"), None);
    }

    #[test]
    fn l1_update_renews_hard_expiry_from_the_update() {
        let cache = L1Cache::new(16);
        cache.set("refresh", b"old", Duration::from_millis(100));
        std::thread::sleep(Duration::from_millis(75));

        // Updating an existing moka entry used to retain the original
        // create deadline. The replacement must receive its full TTL.
        cache.set("refresh", b"new", Duration::from_millis(300));
        std::thread::sleep(Duration::from_millis(100));
        cache.run_pending_tasks();
        assert_eq!(cache.get("refresh"), Some(b"new".to_vec()));

        std::thread::sleep(Duration::from_millis(250));
        cache.run_pending_tasks();
        assert_eq!(cache.get("refresh"), None);
    }

    #[test]
    fn l1_capacity_eviction() {
        let cache = L1Cache::new(2);
        cache.set("a", b"1", Duration::from_secs(60));
        cache.set("b", b"2", Duration::from_secs(60));
        cache.set("c", b"3", Duration::from_secs(60));
        cache.run_pending_tasks();
        let count = ["a", "b", "c"]
            .iter()
            .filter(|k| cache.get(k).is_some())
            .count();
        assert!(count <= 2, "Expected at most 2 entries, got {count}");
    }
}