rs-cache-kit
Generic LRU cache with TTL, tags, and async support for Rust
Installation
[]
= "0.4.3"
Usage
use Cache;
use Duration;
let cache = new;
cache.set;
let val = cache.get; // Some("value")
Custom TTL and Tags
cache.set_with;
Tag-Based Invalidation
let removed = cache.invalidate_by_tag;
println!;
Thread Safety
The cache is Clone and uses Arc<RwLock<...>> internally — safe to share across threads.
let cache = new;
let cache2 = cache.clone;
spawn;
Other Operations
cache.has // check existence
cache.delete // delete entry
cache.size // entry count
cache.clear // remove all
Get or Insert
let value = cache.get_or_insert_with;
Cache Stats
Track hit/miss/eviction counters for monitoring and tuning:
let cache = new;
cache.set;
cache.get; // hit
cache.get; // miss
let stats = cache.stats;
println!;
Batch Get
Retrieve multiple keys in one call:
cache.set;
cache.set;
let results = cache.get_many;
// returns HashMap with "x" => 1, "y" => 2 (missing keys omitted)
Conditional Delete
Remove entries matching a predicate:
cache.set;
cache.set;
let removed = cache.delete_where;
// removed == 1, "big" is gone
Maintenance
cache.len // entry count (alias for size)
cache.is_empty // check if empty
cache.max_size // max capacity
cache.keys // all non-expired keys
cache.remove_expired // clean up expired entries
API
| Function / Type | Description |
|---|---|
Cache::new(max_size, default_ttl) |
Create a new cache with max capacity and optional default TTL |
Cache::default() |
Create a cache with max_size=100 and no TTL |
cache.set(key, value) |
Insert a value with default TTL and no tags |
cache.set_with(key, value, ttl, tags) |
Insert a value with custom TTL and tags |
cache.get(key) |
Get a value (returns None if missing or expired) |
cache.get_many(keys) |
Retrieve multiple values at once |
cache.get_or_insert_with(key, f) |
Get or compute and insert a value |
cache.has(key) |
Check if a key exists and is not expired |
cache.delete(key) |
Delete an entry by key |
cache.delete_where(predicate) |
Remove entries matching a predicate |
cache.invalidate_by_tag(tag) |
Remove all entries with the given tag |
cache.clear() |
Remove all entries |
cache.size() / cache.len() |
Return the number of entries |
cache.is_empty() |
Check if the cache is empty |
cache.max_size() |
Return the max capacity |
cache.keys() |
Return all non-expired keys |
cache.remove_expired() |
Clean up expired entries |
cache.stats() |
Return hit/miss/eviction counters as CacheStats |
CacheStats |
Struct with hits, misses, evictions fields |
Development
Support
If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.