Skip to content

gamandeepsingh/quickcache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

quickcache

A lightweight in-memory LRU cache with per-entry TTL (time-to-live) expiry for Rust.

Features

  • Generic over any Clone value 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

Installation

Add to your Cargo.toml:

[dependencies]
quickcache = "0.1.0"

Usage

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();
}

API

Cache::new(capacity: usize) -> Cache<T>

Creates a new cache with the given maximum number of entries.

set(&mut self, key: &str, value: T, ttl_secs: u64)

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.

get(&mut self, key: &str) -> Option<T>

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.

invalidate(&mut self, key: &str)

Removes a single entry by key, regardless of its TTL.

clear(&mut self)

Removes all entries from the cache.

License

MIT

About

A lightweight in-memory LRU cache with per-entry TTL (time-to-live) expiry for Rust.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages