NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
lru_cache.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
2#define NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
3
11
16NEFORCE_BEGIN_NAMESPACE__
17
23
40template <typename Key, typename Value>
41class lru_cache {
42public:
43 using key_type = Key;
44 using value_type = Value;
45 using size_type = size_t;
49
50private:
51 using list_type = list<pair<Key, Value>>;
52 using list_iterator = typename list_type::iterator;
53
54 size_type capacity_;
55 list_type list_;
57 unordered_map<Key, time_point> access_times_;
58
59public:
68 capacity_(capacity) {
69 if (capacity_ == 0) {
70 NEFORCE_THROW_EXCEPTION(value_exception("lru_cache capacity must be positive"));
71 }
72 }
73
74 lru_cache(const lru_cache&) = delete;
75 lru_cache& operator=(const lru_cache&) = delete;
76 lru_cache(lru_cache&&) = default;
77 lru_cache& operator=(lru_cache&&) = default;
78
88 void put(const Key& key, const Value& value) {
89 auto it = map_.find(key);
90 if (it != map_.end()) {
91 it->second->second = value;
92 list_.splice(list_.begin(), list_, it->second);
93 } else {
94 if (list_.size() >= capacity_) {
95 auto last = --list_.end();
96 map_.erase(last->first);
97 access_times_.erase(last->first);
98 list_.pop_back();
99 }
100 list_.emplace_front(key, value);
101 map_[key] = list_.begin();
102 }
103 access_times_[key] = clock::now();
104 }
105
113 NEFORCE_NODISCARD optional<Value> get(const Key& key) {
114 auto it = map_.find(key);
115 if (it == map_.end()) {
116 return none;
117 }
118 list_.splice(list_.begin(), list_, it->second);
119 access_times_[key] = clock::now();
120 return optional<Value>{it->second->second};
121 }
122
131 NEFORCE_NODISCARD optional<Value> peek(const Key& key) const {
132 auto it = map_.find(key);
133 if (it == map_.end()) {
134 return none;
135 }
136 return optional<Value>{it->second->second};
137 }
138
144 bool erase(const Key& key) {
145 auto it = map_.find(key);
146 if (it == map_.end()) {
147 return false;
148 }
149 list_.erase(it->second);
150 map_.erase(it);
151 return true;
152 }
153
161 void remove_expired(duration max_age) {
162 auto now = clock::now();
163 auto it = access_times_.begin();
164 while (it != access_times_.end()) {
165 if (now - it->second > max_age) {
166 erase(it->first);
167 it = access_times_.erase(it);
168 } else {
169 ++it;
170 }
171 }
172 }
173
177 void clear() {
178 list_.clear();
179 map_.clear();
180 }
181
186 NEFORCE_NODISCARD size_type size() const noexcept { return list_.size(); }
187
192 NEFORCE_NODISCARD size_type capacity() const noexcept { return capacity_; }
193
199 NEFORCE_NODISCARD bool contains(const Key& key) const noexcept { return map_.find(key) != map_.end(); }
200
208 template <typename Predicate>
209 void remove_if(Predicate pred) {
210 for (auto it = list_.begin(); it != list_.end();) {
211 if (pred(*it)) {
212 map_.erase(it->first);
213 it = list_.erase(it);
214 } else {
215 ++it;
216 }
217 }
218 }
219};
220 // Cache
222
223NEFORCE_END_NAMESPACE__
224#endif // NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
双向链表容器
list_iterator< false, list > iterator
LRU缓存类模板
Key key_type
键类型
void put(const Key &key, const Value &value)
插入或更新缓存项
size_type capacity() const noexcept
获取缓存容量
Value value_type
值类型
optional< Value > peek(const Key &key) const
查看缓存项(不更新访问状态)
void remove_expired(duration max_age)
删除过期的缓存项
size_t size_type
大小类型
lru_cache(size_type capacity)
构造函数
void remove_if(Predicate pred)
按条件删除缓存项
void clear()
清空所有缓存项
optional< Value > get(const Key &key)
获取缓存项
clock::duration duration
持续时间类型
bool erase(const Key &key)
删除缓存项
bool contains(const Key &key) const noexcept
检查缓存是否包含指定键
clock::time_point time_point
时间点类型
steady_clock clock
时钟类型
size_type size() const noexcept
获取当前缓存大小
可选值类
无序映射容器
时钟类型
constexpr none_t none
默认空表示
uint64_t size_t
无符号大小类型
双向链表容器
可选值类型
稳定时钟
static time_point now() noexcept
获取当前时间点
nanoseconds duration
持续时间类型
_NEFORCE time_point< steady_clock > time_point
时间点类型
变量处理异常
无序映射容器