Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

C++ cache with LRU/LFU/FIFO policies implementation

License

NotificationsYou must be signed in to change notification settings

vpetrigo/caches

Repository files navigation

CIBuild statuscodecovversion

C++ Cache implementation

This project implements a simple thread-safe cache with several page replacement policies:

  • Least Recently Used
  • First-In/First-Out
  • Least Frequently Used

More about cache algorithms and policy you could read onWikipedia

Usage

Using this library is simple. It is necessary to include header with the cache implementation (cache.hpp file)and appropriate header with the cache policy if it is needed. If not then the non-special algorithm will be used (itremoves the last element which key is the last in the internal container).

Currently, there is only three of them:

  • fifo_cache_policy.hpp
  • lfu_cache_policy.hpp
  • lru_cache_policy.hpp

Example for the LRU policy:

#include<string>#include"cache.hpp"#include"lru_cache_policy.hpp"// alias for an easy class typingtemplate<typename Key,typename Value>usinglru_cache_t =typename caches::fixed_sized_cache<Key, Value, caches::LRUCachePolicy>;voidfoo() {constexpr std::size_t CACHE_SIZE =256;lru_cache_t<std::string,int>cache(CACHE_SIZE);  cache.Put("Hello",1);  cache.Put("world",2);constauto hello_value = cache.Get("Hello");constauto world_value = cache.Get("world");  std::cout << *hello_value << *world_value <<'\n';// "12"}

Custom hashmap usage

You can use a custom hashmap implementation for thecaches::fixed_sized_cache class which has the same interfacestd::unordered_map has.

For example, you can declare LRU cache type like that:

template<typename Key,typename Value>usinglru_cache_t =typename caches::fixed_sized_cache<Key, Value, caches::LRUCachePolicy,                                                       phmap::node_hash_map<Key, Value>>;// ...lru_cache_t<std::string, std::size_t> cache{16};cache.Put("Hello",1);std::cout << *cache.Get("Hello") <<'\n';

Seetest implementation which usesparallel-hashmap.

Requirements

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.

Contributing

Please fork this repository and contribute back usingpull requests.Features can be requested usingissues. All code, comments, and critiquesare greatly appreciated.


[8]ページ先頭

©2009-2025 Movatter.jp