Cache System is a C++14 project implementing high-performance key-value caches. One of them is an LRU and LFU cache.
LRU Cache is a least recently used cache that stores key-value pairs and automatically removes the least recently accessed elements when the cache reaches its capacity.
LFU Cache stores key-value pairs and automatically removes the least frequently used elements when the cache reaches its capacity. Elements with higher access frequency remain in the cache longer, while new or rarely accessed elements are removed first.
The caches is implemented in C++14 using a doubly linked list for ordering and either std::unordered_map or std::map for fast lookups:
- If the keys are hashable —
std::unordered_mapfor O(1) access. - If the keys are not hashable —
std::mapfor O(log N) access.
- Insert elements by copy, move, or emplace (construct in-place).
- Access elements either moving them to the front (
get) or without changing their position (peek). - Manual removal (
erase) and clearing of the cache (clear). - Dynamic resizing of capacity (
set_capacity). - Status checks:
contains,empty,full,size,capacity. - Throws exceptions when accessing a non-existent key (
KeyNotFound).
- C++14, templates, manual memory management.
- Own doubly linked list + map for maximum performance and flexibility.
- Choice of map depends on hash availability:
- Hashable →
std::unordered_map - Non-hashable →
std::map
- Hashable →
mkdir build && cd build
cmake ..
cmake --build .
ctest