NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
lru_cache< Key, Value > 模板类 参考

LRU缓存类模板 更多...

#include <lru_cache.hpp>

Public 类型

using key_type = Key
 键类型
using value_type = Value
 值类型
using size_type = size_t
 大小类型
using clock = steady_clock
 时钟类型
using time_point = clock::time_point
 时间点类型
using duration = clock::duration
 持续时间类型

Public 成员函数

 lru_cache (size_type capacity)
 构造函数
void put (const Key &key, const Value &value)
 插入或更新缓存项
NEFORCE_NODISCARD optional< Value > get (const Key &key)
 获取缓存项
NEFORCE_NODISCARD optional< Value > peek (const Key &key) const
 查看缓存项(不更新访问状态)
bool erase (const Key &key)
 删除缓存项
void remove_expired (duration max_age)
 删除过期的缓存项
void clear ()
 清空所有缓存项
NEFORCE_NODISCARD size_type size () const noexcept
 获取当前缓存大小
NEFORCE_NODISCARD size_type capacity () const noexcept
 获取缓存容量
NEFORCE_NODISCARD bool contains (const Key &key) const noexcept
 检查缓存是否包含指定键
template<typename Predicate>
void remove_if (Predicate pred)
 按条件删除缓存项

详细描述

template<typename Key, typename Value>
class lru_cache< Key, Value >

LRU缓存类模板

模板参数
Key键类型
Value值类型

实现LRU(最近最少使用)缓存淘汰策略的容器。 当缓存达到容量上限时,会淘汰最长时间未被访问的条目。

特性:

  • O(1)时间复杂度的插入和访问
  • 支持缓存项过期时间
  • 支持条件删除
注解
此类不是线程安全的,多线程环境下需要外部同步

在文件 lru_cache.hpp39 行定义.

构造及析构函数说明

◆ lru_cache()

template<typename Key, typename Value>
lru_cache< Key, Value >::lru_cache ( size_type capacity)
inlineexplicit

构造函数

参数
capacity缓存容量
异常
value_exception当capacity为0时抛出

创建一个指定容量的LRU缓存。

在文件 lru_cache.hpp65 行定义.

引用了 capacity().

成员函数说明

◆ capacity()

template<typename Key, typename Value>
NEFORCE_NODISCARD size_type lru_cache< Key, Value >::capacity ( ) const
inlinenoexcept

获取缓存容量

返回
缓存的最大容量

在文件 lru_cache.hpp190 行定义.

被这些函数引用 lru_cache().

◆ contains()

template<typename Key, typename Value>
NEFORCE_NODISCARD bool lru_cache< Key, Value >::contains ( const Key & key) const
inlinenoexcept

检查缓存是否包含指定键

参数
key
返回
如果存在返回true,否则返回false

在文件 lru_cache.hpp197 行定义.

◆ erase()

template<typename Key, typename Value>
bool lru_cache< Key, Value >::erase ( const Key & key)
inline

删除缓存项

参数
key
返回
如果键存在并成功删除返回true,否则返回false

在文件 lru_cache.hpp142 行定义.

被这些函数引用 remove_expired().

◆ get()

template<typename Key, typename Value>
NEFORCE_NODISCARD optional< Value > lru_cache< Key, Value >::get ( const Key & key)
inline

获取缓存项

参数
key
返回
包含值的optional,如果键不存在则返回none

访问缓存项会将其移动到最近使用位置,并更新最后访问时间。

在文件 lru_cache.hpp111 行定义.

引用了 none , 以及 steady_clock::now().

◆ peek()

template<typename Key, typename Value>
NEFORCE_NODISCARD optional< Value > lru_cache< Key, Value >::peek ( const Key & key) const
inline

查看缓存项(不更新访问状态)

参数
key
返回
包含值的optional,如果键不存在则返回none

与get不同,此方法不会移动缓存项到最近使用位置,也不会更新访问时间。 适用于仅需要查看而不改变缓存状态的场景。

在文件 lru_cache.hpp129 行定义.

引用了 none.

◆ put()

template<typename Key, typename Value>
void lru_cache< Key, Value >::put ( const Key & key,
const Value & value )
inline

插入或更新缓存项

参数
key
value

如果键已存在,更新其值并将其移动到最近使用位置; 如果键不存在且缓存已满,淘汰最久未使用的项后再插入新项; 无论插入还是更新,都会更新该键的最后访问时间。

在文件 lru_cache.hpp86 行定义.

引用了 steady_clock::now().

◆ remove_expired()

template<typename Key, typename Value>
void lru_cache< Key, Value >::remove_expired ( duration max_age)
inline

删除过期的缓存项

参数
max_age最大存活时间

遍历所有缓存项,删除最后访问时间超过max_age的项。 适用于需要自动清理过期数据的场景。

在文件 lru_cache.hpp159 行定义.

引用了 erase() , 以及 steady_clock::now().

◆ remove_if()

template<typename Key, typename Value>
template<typename Predicate>
void lru_cache< Key, Value >::remove_if ( Predicate pred)
inline

按条件删除缓存项

模板参数
Predicate谓词类型
参数
pred判断函数,返回true时删除对应项

遍历所有缓存项,删除满足谓词条件的项。

在文件 lru_cache.hpp207 行定义.

◆ size()

template<typename Key, typename Value>
NEFORCE_NODISCARD size_type lru_cache< Key, Value >::size ( ) const
inlinenoexcept

获取当前缓存大小

返回
缓存中的元素数量

在文件 lru_cache.hpp184 行定义.


该类的文档由以下文件生成: