带有 LRU/LFU/FIFO 策略的 C++ 缓存实现
This project implements a simple thread-safe cache with several page replacement policies:
More about cache algorithms and policy you could read on Wikipedia
Using this library is simple. It is necessary to include header with the cache implementation (caches/cache.hpp file)
and appropriate header with the cache policy if it is needed. If not then the non-special algorithm will be used (it
removes the last element which key is the last in the internal container).
Currently, there is only three of them:
fifo_cache_policy.hpplfu_cache_policy.hpplru_cache_policy.hppExample for the LRU policy:
#include
#include "caches/cache.hpp"
#include "caches/lru_cache_policy.hpp"
// alias for an easy class typing
template
using lru_cache_t = typename caches::fixed_sized_cache;
void foo() {
constexpr std::size_t CACHE_SIZE = 256;
lru_cache_t cache(CACHE_SIZE);
cache.Put("Hello", 1);
cache.Put("world", 2);
const auto hello_value = cache.Get("Hello");
const auto world_value = cache.Get("world");
std::cout
using lru_cache_t = typename caches::fixed_sized_cache>;
// ...
lru_cache_t cache{16};
cache.Put("Hello", 1);
std::cout << *cache.Get("Hello") << '\n';See test implementation which uses parallel-hashmap.
The only requirement is a compatible C++11 compiler.
This project was tested in the environments listed below:
If you have any issues with the library building, please let me know.
Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are greatly appreciated.
暂无开放 Issues,或尚未同步最近议题。