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

TTL缓存类模板 更多...

#include <ttl_cache.hpp>

Public 类型

enum class  refresh_policy : uint8_t { never , on_access , sliding_window }
 过期时间刷新策略 更多...
using clock = steady_clock
 时钟类型
using time_point = clock::time_point
 时间点类型
using duration = clock::duration
 持续时间类型
using size_type = size_t
 大小类型

Public 成员函数

 ttl_cache (size_type capacity, duration default_ttl=seconds(60))
 构造函数
 ~ttl_cache ()
 析构函数
void enable_cleanup (duration interval=seconds(1))
 启用后台清理线程
void disable_cleanup ()
 禁用后台清理线程
void set_refresh_policy (refresh_policy policy)
 设置刷新策略
void put (const Key &key, const Value &value)
 插入缓存项(使用默认TTL)
void put (const Key &key, const Value &value, duration ttl)
 插入缓存项(指定TTL)
NEFORCE_NODISCARD optional< Value > get (const Key &key)
 获取缓存项
NEFORCE_NODISCARD bool contains (const Key &key)
 检查缓存是否包含指定键
bool erase (const Key &key)
 删除缓存项
void clear ()
 清空所有缓存项
NEFORCE_NODISCARD size_type size () const noexcept
 获取当前缓存大小
NEFORCE_NODISCARD size_type capacity () const noexcept
 获取缓存容量
void cleanup ()
 手动清理过期项

详细描述

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

TTL缓存类模板

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

实现带有生存时间(TTL)的缓存容器,基于LRU缓存实现。 每个缓存项都有独立的过期时间,过期后自动失效。 支持可选的自动清理线程,定时清理过期项。

特性:

  • 自动过期:每个缓存项有独立的TTL
  • 过期清理:支持后台线程自动清理过期项
  • 刷新策略:支持多种过期时间刷新策略
  • 继承LRU特性:满容量时淘汰最久未使用的项
注解
此类不是线程安全的,多线程环境下需要外部同步

在文件 ttl_cache.hpp44 行定义.

成员枚举类型说明

◆ refresh_policy

template<typename Key, typename Value>
enum class ttl_cache::refresh_policy : uint8_t
strong

过期时间刷新策略

枚举值
never 

不刷新,保持原始过期时间

on_access 

访问时刷新,每次访问都重置过期时间

sliding_window 

滑动窗口,每次访问延长TTL

在文件 ttl_cache.hpp55 行定义.

构造及析构函数说明

◆ ttl_cache()

template<typename Key, typename Value>
ttl_cache< Key, Value >::ttl_cache ( size_type capacity,
duration default_ttl = seconds(60) )
inlineexplicit

构造函数

参数
capacity缓存容量
default_ttl默认生存时间,默认为60秒

创建一个指定容量和默认TTL的缓存。

在文件 ttl_cache.hpp89 行定义.

引用了 capacity().

◆ ~ttl_cache()

template<typename Key, typename Value>
ttl_cache< Key, Value >::~ttl_cache ( )
inline

析构函数

自动停止并等待清理线程结束。

在文件 ttl_cache.hpp98 行定义.

引用了 disable_cleanup().

成员函数说明

◆ capacity()

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

获取缓存容量

返回
缓存的最大容量

在文件 ttl_cache.hpp238 行定义.

被这些函数引用 ttl_cache().

◆ cleanup()

template<typename Key, typename Value>
void ttl_cache< Key, Value >::cleanup ( )
inline

手动清理过期项

遍历所有缓存项,删除已过期的项。 此方法在启用后台清理线程时会自动定期调用。

在文件 ttl_cache.hpp246 行定义.

引用了 steady_clock::now() , 以及 pair< T1, T2 >::second.

被这些函数引用 enable_cleanup().

◆ contains()

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

检查缓存是否包含指定键

参数
key
返回
如果键存在且未过期返回true,否则返回false

此方法会检查过期时间,如果键已过期则自动删除。

在文件 ttl_cache.hpp203 行定义.

引用了 steady_clock::now().

◆ disable_cleanup()

template<typename Key, typename Value>
void ttl_cache< Key, Value >::disable_cleanup ( )
inline

禁用后台清理线程

停止后台清理线程并等待其结束。 不会清空已有缓存项,只是停止自动清理。

在文件 ttl_cache.hpp128 行定义.

被这些函数引用 ~ttl_cache().

◆ enable_cleanup()

template<typename Key, typename Value>
void ttl_cache< Key, Value >::enable_cleanup ( duration interval = seconds(1))
inline

启用后台清理线程

参数
interval清理间隔,默认为1秒

启动后台线程定期清理过期的缓存项。 如果清理线程已运行,此操作无效。

在文件 ttl_cache.hpp107 行定义.

引用了 cleanup().

◆ erase()

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

删除缓存项

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

在文件 ttl_cache.hpp221 行定义.

◆ get()

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

获取缓存项

参数
key
返回
包含值的optional,如果键不存在或已过期返回none

根据刷新策略决定是否更新过期时间。

在文件 ttl_cache.hpp175 行定义.

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

◆ put() [1/2]

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

插入缓存项(使用默认TTL)

参数
key
value

使用默认TTL插入或更新缓存项。

在文件 ttl_cache.hpp150 行定义.

引用了 steady_clock::now().

◆ put() [2/2]

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

插入缓存项(指定TTL)

参数
key
value
ttl生存时间

使用指定的TTL插入或更新缓存项。

在文件 ttl_cache.hpp163 行定义.

引用了 steady_clock::now().

◆ set_refresh_policy()

template<typename Key, typename Value>
void ttl_cache< Key, Value >::set_refresh_policy ( refresh_policy policy)
inline

设置刷新策略

参数
policy新的刷新策略

决定访问缓存项时如何影响其过期时间。

在文件 ttl_cache.hpp141 行定义.

◆ size()

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

获取当前缓存大小

返回
缓存中的元素数量

在文件 ttl_cache.hpp232 行定义.


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