1#ifndef NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
2#define NEFORCE_CORE_CONTAINER_LRU_CACHE_HPP__
14NEFORCE_BEGIN_NAMESPACE__
38template <
typename Key,
typename Value>
68 NEFORCE_THROW_EXCEPTION(
value_exception(
"lru_cache capacity must be positive"));
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);
92 if (list_.size() >= capacity_) {
93 auto last = --list_.end();
94 map_.erase(last->first);
95 access_times_.erase(last->first);
98 list_.emplace_front(key, value);
99 map_[key] = list_.begin();
112 auto it = map_.find(key);
113 if (it == map_.end()) {
116 list_.splice(list_.begin(), list_, it->second);
130 auto it = map_.find(key);
131 if (it == map_.end()) {
143 auto it = map_.find(key);
144 if (it == map_.end()) {
147 list_.erase(it->second);
161 auto it = access_times_.begin();
162 while (it != access_times_.end()) {
163 if (now - it->second > max_age) {
165 it = access_times_.erase(it);
197 NEFORCE_NODISCARD
bool contains(
const Key& key)
const noexcept {
return map_.find(key) != map_.end(); }
206 template <
typename Predicate>
208 for (
auto it = list_.begin(); it != list_.end();) {
210 map_.erase(it->first);
211 it = list_.erase(it);
221NEFORCE_END_NAMESPACE__
list_iterator< false, list > iterator
void put(const Key &key, const Value &value)
插入或更新缓存项
void remove_expired(duration max_age)
删除过期的缓存项
lru_cache(size_type capacity)
构造函数
NEFORCE_NODISCARD optional< Value > get(const Key &key)
获取缓存项
void remove_if(Predicate pred)
按条件删除缓存项
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
时间点类型
NEFORCE_NODISCARD size_type size() const noexcept
获取当前缓存大小
NEFORCE_NODISCARD optional< Value > peek(const Key &key) const
查看缓存项(不更新访问状态)
NEFORCE_INLINE17 constexpr none_t none
默认空表示
static time_point now() noexcept
获取当前时间点
nanoseconds duration
持续时间类型
_NEFORCE time_point< steady_clock > time_point
时间点类型