A lightweight in-memory LRU cache with per-entry TTL (time-to-live) expiry for Rust.
- Generic over any
Clonevalue type - Bounded capacity with LRU eviction — least recently used entry is dropped when full
- Per-entry TTL expiry (checked lazily on read)
- Manual invalidation by key or full cache clear
- Zero dependencies
Add to your Cargo.toml:
[dependencies]
quickcache = "0.1.0"use quickcache::Cache;
fn main() {
// Create a cache that holds at most 3 entries
let mut cache: Cache<String> = Cache::new(3);
// Store values with a TTL in seconds
cache.set("user:1", String::from("Alice"), 60);
cache.set("user:2", String::from("Bob"), 60);
cache.set("user:3", String::from("Carol"), 60);
// Retrieve a value — also bumps it to the front of the LRU order
if let Some(name) = cache.get("user:1") {
println!("Found: {}", name);
}
// Adding a 4th entry evicts the least recently used one ("user:2")
cache.set("user:4", String::from("Dave"), 60);
// Remove a single entry
cache.invalidate("user:3");
// Wipe everything
cache.clear();
}Creates a new cache with the given maximum number of entries.
Inserts or overwrites a key with the given value and TTL in seconds. If the cache is at capacity, the least recently used entry is evicted.
Returns a clone of the value if the key exists and has not expired. Marks the entry as most recently used. Expired entries are removed on access.
Removes a single entry by key, regardless of its TTL.
Removes all entries from the cache.
MIT