NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
ttl_cache.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
2#define NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
3
12
18NEFORCE_BEGIN_NAMESPACE__
19
25
44template <typename Key, typename Value>
45class ttl_cache {
46public:
50 using size_type = size_t;
51
56 enum class refresh_policy : uint8_t {
57 never,
58 on_access,
59 sliding_window
60 };
61
62private:
69 struct entry {
70 Value value;
71 time_point expiry;
72 };
73
74 lru_cache<Key, entry> cache_;
75 duration default_ttl_;
76
77 atomic<bool> running_{false};
78 refresh_policy refresh_policy_{refresh_policy::never};
79 duration cleanup_interval_{seconds(1)};
80 thread cleanup_thread_;
81 mutable mutex cv_mutex_;
82 condition_variable cv_;
83
84public:
92 explicit ttl_cache(size_type capacity, duration default_ttl = seconds(60)) :
93 cache_(capacity),
94 default_ttl_(default_ttl) {}
95
102
110 void enable_cleanup(duration interval = seconds(1)) {
111 if (running_) {
112 return;
113 }
114
115 cleanup_interval_ = interval;
116 running_ = true;
117 cleanup_thread_ = thread([this] {
118 while (running_) {
119 unique_lock<mutex> lk(cv_mutex_);
120 if (cv_.wait_for(lk, cleanup_interval_) == cv_status::timeout) {
121 lk.unlock_quiet();
122 cleanup();
123 }
124 }
125 });
126 }
127
135 running_ = false;
136 cv_.notify_one();
137 if (cleanup_thread_.joinable()) {
138 cleanup_thread_.join();
139 }
140 }
141
148 void set_refresh_policy(refresh_policy policy) { refresh_policy_ = policy; }
149
157 void put(const Key& key, const Value& value) {
158 time_point expiry = clock::now() + default_ttl_;
159 cache_.put(key, entry{value, expiry});
160 }
161
170 void put(const Key& key, const Value& value, duration ttl) {
171 time_point expiry = clock::now() + ttl;
172 cache_.put(key, entry{value, expiry});
173 }
174
182 NEFORCE_NODISCARD optional<Value> get(const Key& key) {
183 auto opt_entry = cache_.get(key);
184 if (!opt_entry) {
185 return none;
186 }
187
188 const entry& e = *opt_entry;
189 if (e.expiry < clock::now()) {
190 cache_.erase(key);
191 return none;
192 }
193
194 if (refresh_policy_ == refresh_policy::on_access || refresh_policy_ == refresh_policy::sliding_window) {
195 entry updated_entry = e;
196 updated_entry.expiry = clock::now() + default_ttl_;
197 cache_.put(key, updated_entry);
198 }
199
200 return optional<Value>{e.value};
201 }
202
210 NEFORCE_NODISCARD bool contains(const Key& key) {
211 auto opt_entry = cache_.peek(key);
212 if (!opt_entry) {
213 return false;
214 }
215 const entry& e = *opt_entry;
216 if (e.expiry < clock::now()) {
217 cache_.erase(key);
218 return false;
219 }
220 return true;
221 }
222
228 bool erase(const Key& key) { return cache_.erase(key); }
229
233 void clear() { cache_.clear(); }
234
239 NEFORCE_NODISCARD size_type size() const noexcept { return cache_.size(); }
240
245 NEFORCE_NODISCARD size_type capacity() const noexcept { return cache_.capacity(); }
246
253 void cleanup() {
254 auto now = clock::now();
255 cache_.remove_if([now](const auto& pair) { return pair.second.expiry < now; });
256 }
257};
258 // Cache
260
261NEFORCE_END_NAMESPACE__
262#endif // NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
原子类型完整实现
size_type size() const noexcept
获取当前缓存大小
clock::duration duration
持续时间类型
ttl_cache(size_type capacity, duration default_ttl=seconds(60))
构造函数
void put(const Key &key, const Value &value)
插入缓存项(使用默认TTL)
size_type capacity() const noexcept
获取缓存容量
void enable_cleanup(duration interval=seconds(1))
启用后台清理线程
void put(const Key &key, const Value &value, duration ttl)
插入缓存项(指定TTL)
void set_refresh_policy(refresh_policy policy)
设置刷新策略
void disable_cleanup()
禁用后台清理线程
refresh_policy
过期时间刷新策略
@ on_access
访问时刷新,每次访问都重置过期时间
@ sliding_window
滑动窗口,每次访问延长TTL
bool erase(const Key &key)
删除缓存项
size_t size_type
大小类型
void clear()
清空所有缓存项
clock::time_point time_point
时间点类型
void cleanup()
手动清理过期项
optional< Value > get(const Key &key)
获取缓存项
steady_clock clock
时钟类型
bool contains(const Key &key)
检查缓存是否包含指定键
独占锁管理器模板
void unlock_quiet()
解锁互斥锁
时钟类型
条件变量行为
unsigned char uint8_t
8位无符号整数类型
duration< int64_t > seconds
秒持续时间
constexpr none_t none
默认空表示
uint64_t size_t
无符号大小类型
LRU缓存实现
存储两个值的元组对
T2 second
第二个元素
static time_point now() noexcept
获取当前时间点
_NEFORCE time_point< steady_clock > time_point
时间点类型
nanoseconds duration
持续时间类型
时间点类模板
线程管理类