-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathKLfuCache.h
More file actions
387 lines (324 loc) · 10.5 KB
/
KLfuCache.h
File metadata and controls
387 lines (324 loc) · 10.5 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#pragma once
#include <cmath>
#include <memory>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <vector>
#include "KICachePolicy.h"
namespace KamaCache
{
template<typename Key, typename Value> class KLfuCache;
template<typename Key, typename Value>
class FreqList
{
private:
struct Node
{
int freq; // 访问频次
Key key;
Value value;
std::weak_ptr<Node> pre; // 上一结点改为weak_ptr打破循环引用
std::shared_ptr<Node> next;
Node()
: freq(1), next(nullptr) {}
Node(Key key, Value value)
: freq(1), key(key), value(value), next(nullptr) {}
};
using NodePtr = std::shared_ptr<Node>;
int freq_; // 访问频率
NodePtr head_; // 假头结点
NodePtr tail_; // 假尾结点
public:
explicit FreqList(int n)
: freq_(n)
{
head_ = std::make_shared<Node>();
tail_ = std::make_shared<Node>();
head_->next = tail_;
tail_->pre = head_;
}
bool isEmpty() const
{
return head_->next == tail_;
}
// 提那家结点管理方法
void addNode(NodePtr node)
{
if (!node || !head_ || !tail_)
return;
node->pre = tail_->pre;
node->next = tail_;
tail_->pre.lock()->next = node; // 使用lock()获取shared_ptr
tail_->pre = node;
}
void removeNode(NodePtr node)
{
if (!node || !head_ || !tail_)
return;
if (node->pre.expired() || !node->next)
return;
auto pre = node->pre.lock(); // 使用lock()获取shared_ptr
pre->next = node->next;
node->next->pre = pre;
node->next = nullptr; // 确保显式置空next指针,彻底断开节点与链表的连接
}
NodePtr getFirstNode() const { return head_->next; }
friend class KLfuCache<Key, Value>;
};
template <typename Key, typename Value>
class KLfuCache : public KICachePolicy<Key, Value>
{
public:
using Node = typename FreqList<Key, Value>::Node;
using NodePtr = std::shared_ptr<Node>;
using NodeMap = std::unordered_map<Key, NodePtr>;
KLfuCache(int capacity, int maxAverageNum = 1000000)
: capacity_(capacity), minFreq_(INT8_MAX), maxAverageNum_(maxAverageNum),
curAverageNum_(0), curTotalNum_(0)
{}
~KLfuCache() override = default;
void put(Key key, Value value) override
{
if (capacity_ == 0)
return;
std::lock_guard<std::mutex> lock(mutex_);
auto it = nodeMap_.find(key);
if (it != nodeMap_.end())
{
// 重置其value值
it->second->value = value;
// 找到了直接调整就好了,不用再去get中再找一遍,但其实影响不大
getInternal(it->second, value);
return;
}
putInternal(key, value);
}
// value值为传出参数
bool get(Key key, Value& value) override
{
std::lock_guard<std::mutex> lock(mutex_);
auto it = nodeMap_.find(key);
if (it != nodeMap_.end())
{
getInternal(it->second, value);
return true;
}
return false;
}
Value get(Key key) override
{
Value value;
get(key, value);
return value;
}
// 清空缓存,回收资源
void purge()
{
nodeMap_.clear();
freqToFreqList_.clear();
}
private:
void putInternal(Key key, Value value); // 添加缓存
void getInternal(NodePtr node, Value& value); // 获取缓存
void kickOut(); // 移除缓存中的过期数据
void removeFromFreqList(NodePtr node); // 从频率列表中移除节点
void addToFreqList(NodePtr node); // 添加到频率列表
void addFreqNum(); // 增加平均访问等频率
void decreaseFreqNum(int num); // 减少平均访问等频率
void handleOverMaxAverageNum(); // 处理当前平均访问频率超过上限的情况
void updateMinFreq();
private:
int capacity_; // 缓存容量
int minFreq_; // 最小访问频次(用于找到最小访问频次结点)
int maxAverageNum_; // 最大平均访问频次
int curAverageNum_; // 当前平均访问频次
int curTotalNum_; // 当前访问所有缓存次数总数
std::mutex mutex_; // 互斥锁
NodeMap nodeMap_; // key 到 缓存节点的映射
std::unordered_map<int, FreqList<Key, Value>*> freqToFreqList_;// 访问频次到该频次链表的映射
};
template<typename Key, typename Value>
void KLfuCache<Key, Value>::getInternal(NodePtr node, Value& value)
{
// 找到之后需要将其从低访问频次的链表中删除,并且添加到+1的访问频次链表中,
// 访问频次+1, 然后把value值返回
value = node->value;
// 从原有访问频次的链表中删除节点
removeFromFreqList(node);
node->freq++;
addToFreqList(node);
// 如果当前node的访问频次如果等于minFreq+1,并且其前驱链表为空,则说明
// freqToFreqList_[node->freq - 1]链表因node的迁移已经空了,需要更新最小访问频次
if (node->freq - 1 == minFreq_ && freqToFreqList_[node->freq - 1]->isEmpty())
minFreq_++;
// 总访问频次和当前平均访问频次都随之增加
addFreqNum();
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::putInternal(Key key, Value value)
{
// 如果不在缓存中,则需要判断缓存是否已满
if (nodeMap_.size() == capacity_)
{
// 缓存已满,删除最不常访问的结点,更新当前平均访问频次和总访问频次
kickOut();
}
// 创建新结点,将新结点添加进入,更新最小访问频次
NodePtr node = std::make_shared<Node>(key, value);
nodeMap_[key] = node;
addToFreqList(node);
addFreqNum();
minFreq_ = std::min(minFreq_, 1);
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::kickOut()
{
NodePtr node = freqToFreqList_[minFreq_]->getFirstNode();
removeFromFreqList(node);
nodeMap_.erase(node->key);
decreaseFreqNum(node->freq);
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::removeFromFreqList(NodePtr node)
{
// 检查结点是否为空
if (!node)
return;
auto freq = node->freq;
freqToFreqList_[freq]->removeNode(node);
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::addToFreqList(NodePtr node)
{
// 检查结点是否为空
if (!node)
return;
// 添加进入相应的频次链表前需要判断该频次链表是否存在
auto freq = node->freq;
if (freqToFreqList_.find(node->freq) == freqToFreqList_.end())
{
// 不存在则创建
freqToFreqList_[node->freq] = new FreqList<Key, Value>(node->freq);
}
freqToFreqList_[freq]->addNode(node);
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::addFreqNum()
{
curTotalNum_++;
if (nodeMap_.empty())
curAverageNum_ = 0;
else
curAverageNum_ = curTotalNum_ / nodeMap_.size();
if (curAverageNum_ > maxAverageNum_)
{
handleOverMaxAverageNum();
}
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::decreaseFreqNum(int num)
{
// 减少平均访问频次和总访问频次
curTotalNum_ -= num;
if (nodeMap_.empty())
curAverageNum_ = 0;
else
curAverageNum_ = curTotalNum_ / nodeMap_.size();
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::handleOverMaxAverageNum()
{
if (nodeMap_.empty())
return;
// 当前平均访问频次已经超过了最大平均访问频次,所有结点的访问频次- (maxAverageNum_ / 2)
for (auto it = nodeMap_.begin(); it != nodeMap_.end(); ++it)
{
// 检查结点是否为空
if (!it->second)
continue;
NodePtr node = it->second;
// 先从当前频率列表中移除
removeFromFreqList(node);
// 减少频率
int oldFreq = node->freq;
int decay = maxAverageNum_ / 2;
node->freq -= decay;
if (node->freq < 1)
node->freq = 1;
int delta = node->freq - oldFreq;
curTotalNum_ += delta;
// 添加到新的频率列表
addToFreqList(node);
}
// 更新最小频率
updateMinFreq();
}
template<typename Key, typename Value>
void KLfuCache<Key, Value>::updateMinFreq()
{
minFreq_ = INT8_MAX;
for (const auto& pair : freqToFreqList_)
{
if (pair.second && !pair.second->isEmpty())
{
minFreq_ = std::min(minFreq_, pair.first);
}
}
if (minFreq_ == INT8_MAX)
minFreq_ = 1;
}
// 并没有牺牲空间换时间,他是把原有缓存大小进行了分片。
template<typename Key, typename Value>
class KHashLfuCache
{
public:
KHashLfuCache(size_t capacity, int sliceNum, int maxAverageNum = 10)
: sliceNum_(sliceNum > 0 ? sliceNum : std::thread::hardware_concurrency())
, capacity_(capacity)
{
size_t sliceSize = std::ceil(capacity_ / static_cast<double>(sliceNum_)); // 每个lfu分片的容量
for (int i = 0; i < sliceNum_; ++i)
{
lfuSliceCaches_.emplace_back(new KLfuCache<Key, Value>(sliceSize, maxAverageNum));
}
}
void put(Key key, Value value)
{
// 根据key找出对应的lfu分片
size_t sliceIndex = Hash(key) % sliceNum_;
lfuSliceCaches_[sliceIndex]->put(key, value);
}
bool get(Key key, Value& value)
{
// 根据key找出对应的lfu分片
size_t sliceIndex = Hash(key) % sliceNum_;
return lfuSliceCaches_[sliceIndex]->get(key, value);
}
Value get(Key key)
{
Value value;
get(key, value);
return value;
}
// 清除缓存
void purge()
{
for (auto& lfuSliceCache : lfuSliceCaches_)
{
lfuSliceCache->purge();
}
}
private:
// 将key计算成对应哈希值
size_t Hash(Key key)
{
std::hash<Key> hashFunc;
return hashFunc(key);
}
private:
size_t capacity_; // 缓存总容量
int sliceNum_; // 缓存分片数量
std::vector<std::unique_ptr<KLfuCache<Key, Value>>> lfuSliceCaches_; // 缓存lfu分片容器
};
} // namespace KamaCache