-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachePolicy.h
More file actions
222 lines (183 loc) · 6.89 KB
/
Copy pathCachePolicy.h
File metadata and controls
222 lines (183 loc) · 6.89 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
#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <utility>
// 大厂标准规范:将所有缓存模块统一收拢到 `Cache` 核心命名空间中,确保派生类能正确识别虚函数基类
namespace Cache {
// ============================================================================
// 1. 写入结果
// ============================================================================
enum class CacheWriteResult {
ignored, // 容量为 0,或策略决定忽略本次写入
inserted, // 新插入,未发生淘汰
updated, // key 已存在,更新 value
insertedWithEviction, // 新插入,并触发淘汰
};
// ============================================================================
// 2. 缓存统计信息
// ============================================================================
struct CacheStats {
std::uint64_t hits{0};
std::uint64_t misses{0};
std::uint64_t evictions{0};
[[nodiscard]] std::uint64_t requests() const noexcept
{
return hits + misses;
}
[[nodiscard]] bool empty() const noexcept
{
return requests() == 0 && evictions == 0;
}
[[nodiscard]] double hitRate() const noexcept
{
const std::uint64_t total = requests();
return total == 0
? 0.0
: static_cast<double>(hits) / static_cast<double>(total);
}
[[nodiscard]] double missRate() const noexcept
{
const std::uint64_t total = requests();
return total == 0
? 0.0
: static_cast<double>(misses) / static_cast<double>(total);
}
void reset() noexcept
{
hits = 0;
misses = 0;
evictions = 0;
}
};
// ============================================================================
// 3. 缓存策略基类
//
// 设计原则:
// 1. get(key, value) 是核心接口,用 bool 表达是否命中。
// 2. tryGet(key) 是安全便捷接口,用 optional 避免 miss 和默认值混淆。
// 3. getOrDefault(key, defaultValue) 用于业务允许默认值兜底的场景。
// 4. peek(key, value) 默认不提供真实实现,具体缓存策略可覆盖。
// 5. stats/resetStats 提供统一监控接口,具体缓存策略可覆盖。
// ============================================================================
template <typename Key, typename Value>
class CachePolicy {
public:
virtual ~CachePolicy() = default;
CachePolicy() = default;
CachePolicy(const CachePolicy&) = delete;
CachePolicy& operator=(const CachePolicy&) = delete;
CachePolicy(CachePolicy&&) = delete;
CachePolicy& operator=(CachePolicy&&) = delete;
// ------------------------------------------------------------------------
// 写入接口
//
// 注意:
// 由于这是虚函数接口,不能做完美转发版本:
// template <typename K, typename V> put(K&&, V&&)
//
// 如果具体缓存实现需要移动语义,可以在派生类中额外提供模板 put。
// ------------------------------------------------------------------------
virtual void put(const Key& key, const Value& value) = 0;
// ------------------------------------------------------------------------
// 查询接口
//
// 命中返回 true,并通过 value 输出结果。
// 未命中返回 false。
//
// 对 LRU / LFU 来说,get 通常会改变缓存状态:
// LRU:刷新访问顺序
// LFU:增加访问频率
// ------------------------------------------------------------------------
virtual bool get(const Key& key, Value& value) = 0;
// ------------------------------------------------------------------------
// 安全便捷查询接口
//
// 推荐业务代码优先使用 tryGet,而不是 get(key) 返回默认值。
// ------------------------------------------------------------------------
[[nodiscard]] std::optional<Value> tryGet(const Key& key)
{
Value value{};
if (!get(key, value)) {
return std::nullopt;
}
return value;
}
// ------------------------------------------------------------------------
// 默认值兜底查询接口
//
// 适合业务明确允许默认值的场景。
// ------------------------------------------------------------------------
[[nodiscard]] Value getOrDefault(const Key& key, Value defaultValue = Value{})
{
Value value{};
if (!get(key, value)) {
return defaultValue;
}
return value;
}
// ------------------------------------------------------------------------
// 只读查看接口
//
// peek 和 get 的区别:
// get 可能改变缓存状态;
// peek 不应该改变缓存状态。
//
// 默认实现返回 false。
// 支持 peek 的缓存策略应覆盖该方法。
// ------------------------------------------------------------------------
[[nodiscard]] virtual bool peek(const Key& key, Value& value) const
{
static_cast<void>(key);
static_cast<void>(value);
return false;
}
[[nodiscard]] std::optional<Value> tryPeek(const Key& key) const
{
Value value{};
if (!peek(key, value)) {
return std::nullopt;
}
return value;
}
// ------------------------------------------------------------------------
// 删除与清理
// ------------------------------------------------------------------------
virtual bool erase(const Key& key) = 0;
virtual void clear() = 0;
void purge()
{
clear();
}
// ------------------------------------------------------------------------
// 状态查询
// ------------------------------------------------------------------------
[[nodiscard]] virtual bool contains(const Key& key) const = 0;
[[nodiscard]] virtual std::size_t size() const = 0;
[[nodiscard]] virtual std::size_t capacity() const noexcept = 0;
[[nodiscard]] bool empty() const
{
return size() == 0;
}
// ------------------------------------------------------------------------
// 统计接口
//
// 普通缓存可以不覆盖,默认返回空统计。
// 分片缓存、生产级缓存建议覆盖。
// ------------------------------------------------------------------------
[[nodiscard]] virtual CacheStats stats() const noexcept
{
return CacheStats{};
}
virtual void resetStats() noexcept
{
}
};
// ============================================================================
// 4. 兼容旧命名别名
// ============================================================================
template <typename Key, typename Value>
using KICachePolicy = CachePolicy<Key, Value>;
template <typename Key, typename Value>
using ICachePolicy = CachePolicy<Key, Value>;
} // namespace Cache