A high-performance, concurrency-safe in-memory cache system with TTL support, persistence, and pluggable LRU & LFU eviction policies.
- Thread-safe caching with fine-grained locking.
- Pluggable eviction strategies (LRU, LFU via factory).
- TTL support with background sweeper.
- Disk persistence via JSON serialization.
- GoogleTest suite for unit and integration testing.
- Benchmarking with Google Benchmark.
- Clean architecture with SOLID principles.
- Configurable via external JSON file.
Exposes a thread-safe key-value cache with support for expiration and disk persistence. Allows switching between eviction strategies at runtime using the factory pattern.
graph TD
Main[Main.cpp] -->|load config| Factory[EvictionFactory]
Factory --> Cache[Cache LRU/LFU]
Cache --> Entry[CacheEntry]
Cache --> Sweeper[CacheSweeper]
Cache --> Persistence[PersistenceManager]
Main --> Logger[Logger]
sequenceDiagram
participant Main
participant Cache
participant Sweeper
participant Entry
Main->>Sweeper: Start cleanup thread
loop every interval
Sweeper->>Cache: snapshot()
Sweeper->>Entry: check isExpired()
alt expired
Sweeper->>Cache: remove(key)
end
end
put(key, value, ttl)get(key)remove(key)snapshot()
- Eviction strategy implementations
- Backed by
unordered_map+listor priority structures
- Encapsulates value, TTL, and access metadata
- Returns the appropriate cache policy at runtime
- Saves and loads cache state as JSON
- Background thread that removes expired keys
- Thread-safe logger with log level control
- Factory – Eviction policy switching
- Strategy – TTL vs non-TTL handling
- Adapter – Persistence via JSON interface
- RAII + Scoped Locking – Safe concurrency
mkdir build && cd build
cmake ..
make./cacheitUses config.json to configure:
{
"eviction_policy": "LRU",
"capacity": 50000,
"default_ttl": 10,
"sweep_interval_seconds": 5,
"log_level": "INFO",
"persistence_file": "cache_dump.json"
}To run cache benchmarks:
./benchmarksOutput:
BM_Get/50000 20166475 ns items_per_second=2.47M/s
./tests