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
14NEFORCE_BEGIN_NAMESPACE__
15
21
38template <typename Key, typename Value>
39class lru_cache {
40public:
41 using key_type = Key;
42 using value_type = Value;
43 using size_type = size_t;
47
48private:
49 using list_type = list<pair<Key, Value>>;
50 using list_iterator = typename list_type::iterator;
51
52 size_type capacity_;
53 list_type list_;
55 unordered_map<Key, time_point> access_times_;
56
57public:
66 capacity_(capacity) {
67 if (capacity_ == 0) {
68 NEFORCE_THROW_EXCEPTION(value_exception("lru_cache capacity must be positive"));
69 }
70 }
71
72 lru_cache(const lru_cache&) = delete;
73 lru_cache& operator=(const lru_cache&) = delete;
74 lru_cache(lru_cache&&) = default;
75 lru_cache& operator=(lru_cache&&) = default;
76
86 void put(const Key& key, const Value& value) {
87 auto it = map_.find(key);
88 if (it != map_.end()) {
89 it->second->second = value;
90 list_.splice(list_.begin(), list_, it->second);
91 } else {
92 if (list_.size() >= capacity_) {
93 auto last = --list_.end();
94 map_.erase(last->first);
95 access_times_.erase(last->first);
96 list_.pop_back();
97 }
98 list_.emplace_front(key, value);
99 map_[key] = list_.begin();
100 }
101 access_times_[key] = clock::now();
102 }
103
111 NEFORCE_NODISCARD optional<Value> get(const Key& key) {
112 auto it = map_.find(key);
113 if (it == map_.end()) {
114 return none;
115 }
116 list_.splice(list_.begin(), list_, it->second);
117 access_times_[key] = clock::now();
118 return optional<Value>{it->second->second};
119 }
120
129 NEFORCE_NODISCARD optional<Value> peek(const Key& key) const {
130 auto it = map_.find(key);
131 if (it == map_.end()) {
132 return none;
133 }
134 return optional<Value>{it->second->second};
135 }
136
142 bool erase(const Key& key) {
143 auto it = map_.find(key);
144 if (it == map_.end()) {
145 return false;
146 }
147 list_.erase(it->second);
148 map_.erase(it);
149 return true;
150 }
151
159 void remove_expired(duration max_age) {
160 auto now = clock::now();
161 auto it = access_times_.begin();
162 while (it != access_times_.end()) {
163 if (now - it->second > max_age) {
164 erase(it->first);
165 it = access_times_.erase(it);
166 } else {
167 ++it;
168 }
169 }
170 }
171
175 void clear() {
176 list_.clear();
177 map_.clear();
178 }
179
184 NEFORCE_NODISCARD size_type size() const noexcept { return list_.size(); }
185
190 NEFORCE_NODISCARD size_type capacity() const noexcept { return capacity_; }
191
197 NEFORCE_NODISCARD bool contains(const Key& key) const noexcept { return map_.find(key) != map_.end(); }
198
206 template <typename Predicate>
207 void remove_if(Predicate pred) {
208 for (auto it = list_.begin(); it != list_.end();) {
209 if (pred(*it)) {
210 map_.erase(it->first);
211 it = list_.erase(it);
212 } else {
213 ++it;
214 }
215 }
216 }
217};
218 // Cache
220
221NEFORCE_END_NAMESPACE__
222#endif // NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
双向链表容器
list_iterator< false, list > iterator
LRU缓存类模板
Key key_type
键类型
void put(const Key &key, const Value &value)
插入或更新缓存项
Value value_type
值类型
void remove_expired(duration max_age)
删除过期的缓存项
size_t size_type
大小类型
lru_cache(size_type capacity)
构造函数
NEFORCE_NODISCARD optional< Value > get(const Key &key)
获取缓存项
void remove_if(Predicate pred)
按条件删除缓存项
void clear()
清空所有缓存项
NEFORCE_NODISCARD bool contains(const Key &key) const noexcept
检查缓存是否包含指定键
clock::duration duration
持续时间类型
NEFORCE_NODISCARD size_type capacity() const noexcept
获取缓存容量
bool erase(const Key &key)
删除缓存项
clock::time_point time_point
时间点类型
steady_clock clock
时钟类型
NEFORCE_NODISCARD size_type size() const noexcept
获取当前缓存大小
NEFORCE_NODISCARD optional< Value > peek(const Key &key) const
查看缓存项(不更新访问状态)
可选值类
无序映射容器
NEFORCE_INLINE17 constexpr none_t none
默认空表示
uint64_t size_t
无符号大小类型
双向链表容器
稳定时钟
static time_point now() noexcept
获取当前时间点
nanoseconds duration
持续时间类型
_NEFORCE time_point< steady_clock > time_point
时间点类型
变量处理异常
无序映射容器