Skip to content

A minimalist, thread-safe, FIFO (First In, First Out) cache with TTL (Time To Live) support for Rust.

License

Notifications You must be signed in to change notification settings

ddernon/fifo-cache

Repository files navigation

FIFO Cache with TTL

crates.io license minimum suppported rust version docs.rs pipeline status

A minimalist, thread-safe, FIFO (First In, First Out) cache with TTL (Time To Live) support for Rust.

Features

  • FIFO eviction policy: Oldest entries are removed when capacity is reached
  • TTL support: Entries automatically expire after a specified duration
  • Zero dependencies: Uses only standard library types
  • Thread-safe: Can be wrapped in Arc<RwLock<>> (or Arc<Mutex<>>) for concurrent access
  • TTL and capacity can be modified after cache creation

Usage

Add this to your Cargo.toml:

[dependencies]
fifo-cache = "0.2"

Or, for no TTL:

[dependencies]
fifo-cache = { version = "0.2", default-features = false }

Example

use fifo_cache::FifoCache;
use std::time::Duration;

let mut cache = FifoCache::new(1000, Duration::from_secs(300));
cache.insert("key", "value");

if let Some(value) = cache.get(&"key") {
  println!("Found: {}", value);
}

Testing

To run the tests, use the following command:

cargo test

Windows 7 compatibility

The minimum required Rust version is 1.59. While this is unlikely to change in the foreseeable future, the main objective is to remain at or below Rust 1.77, so as to preserve Windows 7 compatibility.

To test with Rust 1.77:

  • Change version = 4 to version = 3 in Cargo.lock.
  • Install the 1.77 target: rustup install 1.77.0-x86_64-pc-windows-gnu.
  • Then run clippy and the tests as follows:
cargo +1.77 clippy
cargo +1.77 test

Running an example

cargo run --example basic_usage

About

A minimalist, thread-safe, FIFO (First In, First Out) cache with TTL (Time To Live) support for Rust.

Resources

License

Stars

Watchers

Forks

Languages