NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
ttl_cache.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
2#define NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
3
12
17NEFORCE_BEGIN_NAMESPACE__
18
24
43template <typename Key, typename Value>
44class ttl_cache {
45public:
49 using size_type = size_t;
50
60
61private:
68 struct entry {
69 Value value;
70 time_point expiry;
71 };
72
73 lru_cache<Key, entry> cache_;
74 duration default_ttl_;
75
76 atomic<bool> running_{false};
77 refresh_policy refresh_policy_{refresh_policy::never};
78 duration cleanup_interval_{seconds(1)};
79 thread cleanup_thread_;
80
81public:
89 explicit ttl_cache(size_type capacity, duration default_ttl = seconds(60)) :
90 cache_(capacity),
91 default_ttl_(default_ttl) {}
92
99
107 void enable_cleanup(duration interval = seconds(1)) {
108 if (running_) {
109 return;
110 }
111
112 cleanup_interval_ = interval;
113 running_ = true;
114 cleanup_thread_ = thread([this] {
115 while (running_) {
116 this_thread::sleep_for(cleanup_interval_);
117 cleanup();
118 }
119 });
120 }
121
129 running_ = false;
130 if (cleanup_thread_.joinable()) {
131 cleanup_thread_.join();
132 }
133 }
134
141 void set_refresh_policy(refresh_policy policy) { refresh_policy_ = policy; }
142
150 void put(const Key& key, const Value& value) {
151 time_point expiry = clock::now() + default_ttl_;
152 cache_.put(key, entry{value, expiry});
153 }
154
163 void put(const Key& key, const Value& value, duration ttl) {
164 time_point expiry = clock::now() + ttl;
165 cache_.put(key, entry{value, expiry});
166 }
167
175 NEFORCE_NODISCARD optional<Value> get(const Key& key) {
176 auto opt_entry = cache_.get(key);
177 if (!opt_entry) {
178 return none;
179 }
180
181 const entry& e = *opt_entry;
182 if (e.expiry < clock::now()) {
183 cache_.erase(key);
184 return none;
185 }
186
187 if (refresh_policy_ == refresh_policy::on_access || refresh_policy_ == refresh_policy::sliding_window) {
188 entry updated_entry = e;
189 updated_entry.expiry = clock::now() + default_ttl_;
190 cache_.put(key, updated_entry);
191 }
192
193 return optional<Value>{e.value};
194 }
195
203 NEFORCE_NODISCARD bool contains(const Key& key) {
204 auto opt_entry = cache_.peek(key);
205 if (!opt_entry) {
206 return false;
207 }
208 const entry& e = *opt_entry;
209 if (e.expiry < clock::now()) {
210 cache_.erase(key);
211 return false;
212 }
213 return true;
214 }
215
221 bool erase(const Key& key) { return cache_.erase(key); }
222
226 void clear() { cache_.clear(); }
227
232 NEFORCE_NODISCARD size_type size() const noexcept { return cache_.size(); }
233
238 NEFORCE_NODISCARD size_type capacity() const noexcept { return cache_.capacity(); }
239
246 void cleanup() {
247 auto now = clock::now();
248 cache_.remove_if([now](const auto& pair) { return pair.second.expiry < now; });
249 }
250};
251 // Cache
253
254NEFORCE_END_NAMESPACE__
255#endif // NEFORCE_CORE_CONTAINER_TTL_CACHE_HPP__
原子类型完整实现
可选值类
线程类
NEFORCE_NODISCARD size_type size() const noexcept
获取当前缓存大小
void cleanup()
手动清理过期项
NEFORCE_NODISCARD optional< Value > get(const Key &key)
获取缓存项
refresh_policy
过期时间刷新策略
@ on_access
访问时刷新,每次访问都重置过期时间
@ never
不刷新,保持原始过期时间
@ sliding_window
滑动窗口,每次访问延长TTL
clock::time_point time_point
时间点类型
~ttl_cache()
析构函数
size_t size_type
大小类型
void enable_cleanup(duration interval=seconds(1))
启用后台清理线程
steady_clock clock
时钟类型
void clear()
清空所有缓存项
clock::duration duration
持续时间类型
NEFORCE_NODISCARD size_type capacity() const noexcept
获取缓存容量
void put(const Key &key, const Value &value)
插入缓存项(使用默认TTL)
void set_refresh_policy(refresh_policy policy)
设置刷新策略
void put(const Key &key, const Value &value, duration ttl)
插入缓存项(指定TTL)
void disable_cleanup()
禁用后台清理线程
bool erase(const Key &key)
删除缓存项
NEFORCE_NODISCARD bool contains(const Key &key)
检查缓存是否包含指定键
ttl_cache(size_type capacity, duration default_ttl=seconds(60))
构造函数
时钟类型
unsigned char uint8_t
8位无符号整数类型
duration< int64_t > seconds
秒持续时间
NEFORCE_INLINE17 constexpr none_t none
默认空表示
uint64_t size_t
无符号大小类型
LRU缓存实现
存储两个值的元组对
T2 second
第二个元素
稳定时钟
static time_point now() noexcept
获取当前时间点
nanoseconds duration
持续时间类型
_NEFORCE time_point< steady_clock > time_point
时间点类型
时间点类模板
线程管理类