-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXLRUCache.h
More file actions
308 lines (258 loc) · 7.8 KB
/
XLRUCache.h
File metadata and controls
308 lines (258 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#pragma once
#include <cmath>
#include <cstring>
#include <list>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "XCachePolicy.h"
namespace XCache {
template <typename Key, typename Value> class XLRUCache;
template <typename Key, typename Value> class LRUNode {
private:
Key key;
Value value;
size_t accesscount;
std::weak_ptr<LRUNode<Key, Value>> prev;
std::shared_ptr<LRUNode<Key, Value>> next;
public:
LRUNode(Key k, Value v) : key(k), value(v), accesscount(1) {}
Key getKey() const { return key; }
Value getValue() const { return value; }
size_t getAccessCount() const { return accesscount; }
void setValue(const Value &v) { value = v; }
void incrementAccessCount() { accesscount++; }
~LRUNode() = default;
friend class XLRUCache<Key, Value>;
};
template <typename Key, typename Value>
class XLRUCache : public XCachePolicy<Key, Value> {
using LRUNodeType = LRUNode<Key, Value>;
using NodePtr = std::shared_ptr<LRUNodeType>;
using NodeMap = std::unordered_map<Key, NodePtr>;
public:
XLRUCache(int capacity) : capacity(capacity) { initializeList(); }
~XLRUCache() override = default;
void put(Key key, Value value) override {
if (capacity <= 0)
return;
std::lock_guard<std::mutex> lock(mtx);
auto it = nodeMap.find(key);
if (it != nodeMap.end()) {
updateExistingNode(it->second, value);
return;
}
addNewNode(key, value);
}
bool get(Key key, Value &value) override {
std::lock_guard<std::mutex> lock(mtx);
auto it = nodeMap.find(key);
if (it != nodeMap.end()) {
moveToMostRecent(it->second);
value = it->second->getValue();
return true;
}
return false;
}
Value get(Key key) override {
Value v{}; // 值初始化,避免找不到值的时候返回垃圾值
get(key, v);
return v;
}
void remove(Key key) {
std::lock_guard<std::mutex> lock(mtx);
auto it = nodeMap.find(key);
if (it != nodeMap.end()) {
removeNode(it->second);
nodeMap.erase(it);
}
}
size_t size() {
std::lock_guard<std::mutex> lock(mtx);
return nodeMap.size();
}
Key getOldestKey() {
std::lock_guard<std::mutex> lock(mtx);
if (dummyHead->next && dummyHead->next != dummyTail) {
return dummyHead->next->getKey();
}
return Key{};
}
private:
void initializeList() {
dummyHead = std::make_shared<LRUNodeType>(Key(), Value());
dummyTail = std::make_shared<LRUNodeType>(Key(), Value());
dummyHead->next = dummyTail;
dummyTail->prev = dummyHead;
}
void updateExistingNode(NodePtr node, const Value &value) {
node->setValue(value);
moveToMostRecent(node);
}
void addNewNode(Key key, const Value &value) {
if (nodeMap.size() >= capacity) {
evictLeastRecent();
}
NodePtr newNode = std::make_shared<LRUNodeType>(key, value);
insertNode(newNode);
nodeMap[key] = newNode;
}
void moveToMostRecent(NodePtr node) {
removeNode(node);
insertNode(node);
}
void insertNode(NodePtr node) {
node->prev = dummyTail->prev;
node->next = dummyTail;
dummyTail->prev.lock()->next = node;
dummyTail->prev = node;
}
void removeNode(NodePtr node) {
if (!node->prev.expired() && node->next) {
node->prev.lock()->next = node->next;
node->next->prev = node->prev;
node->next = nullptr;
}
}
void evictLeastRecent() {
NodePtr node = dummyHead->next;
removeNode(node);
nodeMap.erase(node->getKey());
}
int capacity;
NodeMap nodeMap;
std::mutex mtx;
NodePtr dummyHead;
NodePtr dummyTail;
};
template <typename Key, typename Value>
class XLRUKCache
: public XLRUCache<
Key,
Value> // LRU优化的K版本,只有在历史访问次数达到K次时,才会将节点移动到主缓存中
{
public:
XLRUKCache(int capacity, int _k = 2, double historyRatio = 2.5)
: XLRUCache<Key, Value>(capacity), // 初始化主缓存的容量
historyList(std::make_unique<XLRUCache<Key, size_t>>(static_cast<int>(
capacity * historyRatio))), // 自动设置历史缓存为容量的2.5倍
k(_k) {}
~XLRUKCache() = default;
bool get(Key key, Value &value) override {
bool inMainCache = XLRUCache<Key, Value>::get(key, value);
size_t historycount = historyList->get(key);
historycount++;
historyList->put(key, historycount);
if (inMainCache) {
return true;
}
if (historycount >= k) // 如果历史访问次数达到K次,将节点移动到主缓存中
{
std::lock_guard<std::mutex> lock(historyMtx);
auto it = historyMap.find(key);
if (it != historyMap.end()) {
Value storedValue = it->second;
historyList->remove(key);
historyMap.erase(it);
XLRUCache<Key, Value>::put(key, storedValue);
value = storedValue;
return true;
}
}
return false;
}
Value get(Key key) {
Value value{}; // 值初始化,避免找不到值的时候返回垃圾值
bool inMainCache = XLRUCache<Key, Value>::get(key, value);
size_t historycount = historyList->get(key);
historycount++;
historyList->put(key, historycount);
if (inMainCache) {
return value;
}
if (historycount >= k) // 如果历史访问次数达到K次,将节点移动到主缓存中
{
std::lock_guard<std::mutex> lock(historyMtx);
auto it = historyMap.find(key);
if (it != historyMap.end()) {
Value storedValue = it->second;
historyList->remove(key);
historyMap.erase(it);
XLRUCache<Key, Value>::put(key, storedValue);
return storedValue;
}
}
return Value{};
}
void put(Key key, Value value) override {
Value existingValue{};
bool inMainCache = XLRUCache<Key, Value>::get(key, existingValue);
if (inMainCache) {
XLRUCache<Key, Value>::put(key, value);
return;
}
size_t historyCount = historyList->get(key);
historyCount++;
// 存储访问次数,value需要另外存储
historyList->put(key, historyCount);
{
std::lock_guard<std::mutex> lock(historyMtx);
historyMap[key] = value;
}
if (historyCount >= k) {
historyList->remove(key);
{
std::lock_guard<std::mutex> lock(historyMtx);
auto it = historyMap.find(key);
if (it != historyMap.end()) {
Value storedValue = it->second;
historyMap.erase(it);
XLRUCache<Key, Value>::put(key, storedValue);
}
}
}
}
private:
int k;
std::unique_ptr<XLRUCache<Key, size_t>> historyList;
std::unordered_map<Key, Value> historyMap;
std::mutex historyMtx; // 为historyMap添加独立的互斥锁
};
template <typename Key, typename Value>
class XHashLRUCaches // 对LRU进行分片操作,提高高并发使用的性能
{
public:
XHashLRUCaches(int cacheSize, int sliceNum)
: cacheSize(cacheSize), sliceNum(sliceNum) {
size_t sliceCapacity = std::ceil(cacheSize / static_cast<double>(sliceNum));
for (int i = 0; i < sliceNum; ++i) {
sliceCaches.emplace_back(new XLRUCache<Key, Value>(sliceCapacity));
}
}
~XHashLRUCaches() = default;
void put(Key key, Value value) {
size_t sliceIndex = Hash(key) % sliceNum;
sliceCaches[sliceIndex]->put(key, value);
}
bool get(Key key, Value &value) {
size_t sliceIndex = Hash(key) % sliceNum;
return sliceCaches[sliceIndex]->get(key, value);
}
Value get(Key key) {
Value value{}; // 值初始化,避免找不到值的时候返回垃圾值
get(key, value);
return value;
}
private:
size_t Hash(Key key) // 对key进行哈希,得到一个哈希值
{
std::hash<Key> hf;
return hf(key);
}
private:
size_t cacheSize; // 总容量
int sliceNum; // 切片数量
std::vector<std::unique_ptr<XLRUCache<Key, Value>>> sliceCaches; // 切片缓存
};
} // namespace XCache