1 unstable release
Uses new Rust 2024
| 0.1.0 | Feb 6, 2026 |
|---|
#331 in Caching
Used in 2 crates
9KB
68 lines
nostro2-cache
Event ID deduplication cache strategies for Nostr relay implementations.
Strategies
1. DashMapCache (Lock-Free)
- Implementation:
dashmap::DashMap(concurrent hashmap) - Pros: Lock-free, excellent concurrent performance
- Cons: No LRU eviction, simple clear-when-full strategy
- Best for: High-throughput, many concurrent writers
2. ParkingLotLruCache
- Implementation:
parking_lot::Mutex<lru::LruCache> - Pros: Fast mutex, automatic LRU eviction, bounded memory
- Cons: Mutex contention under very high concurrency
- Best for: Moderate concurrency with memory constraints
3. StdMutexLruCache
- Implementation:
std::sync::Mutex<lru::LruCache> - Pros: No external deps, automatic LRU eviction
- Cons: Slower than parking_lot under contention
- Best for: Simple use cases, minimal dependencies
Benchmarks
Run benchmarks to compare strategies:
# Run all benchmarks
cargo bench --package nostro2-cache
# Run specific benchmark
cargo bench --package nostro2-cache -- single_thread
cargo bench --package nostro2-cache -- multi_thread
cargo bench --package nostro2-cache -- realistic
# View HTML reports
open target/criterion/report/index.html
Benchmark Scenarios
- Single Thread Insert: Pure insertion performance
- Multi Thread Insert: Concurrent insert with 2, 4, 8, 10, 20 threads
- Realistic Relay Pattern: 10 threads with 20% duplicate rate
Usage
use nostro2_cache::{DashMapCache, ParkingLotLruCache, StdMutexLruCache};
// Choose your strategy
let cache = DashMapCache::new(10_000);
// Check for duplicates
if cache.insert(event_id) {
println!("New event!");
} else {
println!("Duplicate event, skip");
}
Recommendations
- nostro2-ring-relay: Use
DashMapCachefor lock-free consistency - nostro2-relay (async): Use
ParkingLotLruCachefor bounded memory - Low concurrency: Use
StdMutexLruCacheto minimize dependencies
Dependencies
~1MB
~16K SLoC