1 #pragma once
2
3 #include <list>
4 #include <unordered_map>
5 #include <mutex>
6 #include <algorithm>
7 #include <assert.h>
8
9 /**
10 * @brief Template cache with an LRU removal policy.
11 * synchronized and thread-safe
12 * reference from https://github.com/google/leveldb/blob/master/util/cache.cc
13 * https://github.com/paudley/lru_cache
14 */
15
16 #if __cplusplus >= 201703L
17 template< class Key, class Data, size_t (*cap_fun)(const Data&) = [](const Data& data) constexpr ->size_t {return sizeof(data);}, void (*delete_fun)(Data&) = [](Data& data) constexpr{}, class Hash = std::hash<Key> >
18 #else
19 template <typename T>
20 static size_t default_cap_fun(const T& data) {
21 return sizeof(data);
22 }
23
24 template <typename T>
25 static void default_delete_fun(T& data) {}
26
27 template< typename Key, typename Data, size_t (*cap_fun)(const Data&) = default_cap_fun<Data>, void (*delete_fun)(Data&) = default_delete_fun<Data>, typename Hash = std::hash<Key> >
28 #endif
29
30 class RefLRUCache
31 {
32 public:
33 struct Node;
34 typedef std::list<Node> List; // Main cache storage typedef
35 typedef typename List::iterator Node_Iter; // Main cache iterator
36 typedef typename List::const_iterator Node_cIter; // Main cache iterator (const)
37 typedef std::unordered_map<Key, Node_Iter, Hash> Map; // Index typedef
38 typedef typename Map::iterator Map_Iter; // Index iterator
39 typedef typename Map::const_iterator Map_cIter; // Index iterator (const)
40
41 struct Node
42 {
43 Node(Key key_, Data value_, int32_t refs_, bool in_use_, bool in_lru_):
44 key(key_),
45 value(value_),
46 refs(refs_),
47 in_use(in_use_),
48 in_lru(in_lru_),
49 charge(cap_fun(value_)),
50 trash_it()
51 {}
52
53 Key key;
54 Data value;
55 int32_t refs;
56 bool in_use;
57 bool in_lru;
58 size_t charge;
59 Node_Iter trash_it;
60 };
61
62 public:
63 RefLRUCache() = delete;
64 RefLRUCache(const RefLRUCache&) = delete;
65 RefLRUCache& operator= (const RefLRUCache&) = delete;
66 RefLRUCache(RefLRUCache&&) = delete;
67 RefLRUCache& operator= (const RefLRUCache&&) = delete;
68
69 explicit RefLRUCache(uint64_t mem_capacity) :
70 _mem_capacity(mem_capacity),
71 _mem_usage(0)
72 {}
73
74 ~RefLRUCache() {
75 assert(_use.empty());
76 assert(_trash.empty());
77 for_each(_lru.begin(), _lru.end(),
78 [](Node& elem) {
79 elem.in_lru = false;
80 elem.in_use = false;
81 _unref(elem);
82 });
83
84 _lru.clear();
85 _index.clear();
86 }
87
88 /** @brief returns the number of elements.
89 */
90 inline size_t size() const {
91 std::lock_guard<std::mutex> lock(_mutex);
92 return _index.size();
93 }
94
95 inline uint64_t mem_capacity() const {
96 std::lock_guard<std::mutex> lock(_mutex);
97 return _mem_capacity;
98 }
99
100 inline uint64_t mem_usage() const {
101 std::lock_guard<std::mutex> lock(_mutex);
102 return _mem_usage;
103 }
104
105 /** @brief Removes a key-node from the cache.
106 */
107 inline void erase( const Key &key ) {
108 std::unique_lock<std::mutex> lock{_mutex};
109 _finish_erase(key);
110
111 if (_mem_usage <= _mem_capacity && _lru.empty()) {
112 lock.unlock();
113 _space_available.notify_one();
114 }
115 }
116
117 /**
118 * @brief lookup_node_it function with release function ia a pair
119 */
120 inline Node_Iter lookup_node_it(const Key &key) {
121 std::lock_guard<std::mutex> lock(_mutex);
122 Map_Iter miter = _index.find(key);
123 if (miter == _index.end())
124 return Node_Iter();
125
126 _ref(*(miter->second));
127
128 return miter->second;
129 }
130
131 /** @brief release once node
132 */
133 inline void release(Node_Iter node_it) {
134 std::unique_lock<std::mutex> lock{_mutex};
135 _unref(*node_it);
136
137 if (!_lru.empty() && _mem_usage > _mem_capacity) {
138 lock.unlock();
139 _space_available.notify_one();
140 }
141 }
142
143 /**
144 * @brief insert func
主站蜘蛛池模板:
国产成人无码综合亚洲日韩|
果冻传媒董小宛视频|
女同性恋一区二区三区视频|
日韩精品有码中文字幕|
成人亚洲性情网站www在线观看|
国产精品麻豆成人av网|
日本亲近相奷中文字幕|
一本色道久久综合无码人妻
|
狠狠色噜噜狠狠狠狠2021|
国产麻豆放荡av激情演绎|
97se亚洲综合自在线|
国产精品毛片在线看不卡|
亚洲综合中文字幕首页|
亚洲精品动漫免费二区|
欧美乱码卡一卡二卡四卡免费|
久久精品不卡一区二区|
凹凸国产熟女精品视频|
日韩精品福利视频在线观看|
岛国最新亚洲伦理成人|
国产午夜精品久久精品电影|
久久久久久久久18禁秘|
亚洲成人av在线资源网|
好紧好爽午夜视频|
亚洲av乱码久久亚洲精品|
无码AV无码免费一区二区|
日本肥老妇色xxxxx日本老妇
|
国产小受被做到哭咬床单GV|
大地资源网中文第五页|
99网友自拍视频在线|
午夜成人无码免费看网站|
九寨沟县|
94人妻少妇偷人精品|
日韩av综合免费在线|
最新国产精品拍自在线观看|
a国产一区二区免费入口|
99热门精品一区二区三区无码|
国产一区二区三区粉嫩av|
国产精品偷伦费观看一次|
国产亚洲精品福利在线无卡一
|
亚洲人成网线在线播放VA|
99久久国产综合精品色|