设为首页 加入收藏

TOP

POCO C++库学习和分析 -- Cache(一)
2014-11-24 03:30:38 】 浏览:4263
Tags:POCO 学习 分析 Cache
POCO C++库学习和分析 -- Cache
1. Cache概述
在STL::map或者STL::set中,容器的尺寸是没有上限的,数目可以不断的扩充。并且在STL的容器中,元素是不会自动过期的,除非显式的被删除。Poco的Cache可以被看成是STL中容器的一个扩充,容器中的元素会自动过期(即失效)。在Poco实现的Cache框架中,基础的过期策略有两种。一种是LRU(Last Recent Used),另外一种是基于时间的过期(Time based expiration)。在上述两种过期策略之上,还提供了两者之间的混合。
下面是相关的类:
1. LRUCache: 最近使用Cache。在内部维护一个Cache的最大容量M,始终只保存M个元素于Cache内部,当第M+1元素插入Cache中时,最先被放入Cache中的元素将失效。
2. ExpireCache: 时间过期Cache。在内部统一管理失效时间T,当元素插入Cache后,超过时间T,则删除。
3. AccessExpireCache: 时间过期Cache。同ExpireCache不同的是,当元素被访问后,重新开始计算该元素的超时时间,而不是只从元素插入时开始计时。
4. UniqueExpireCache: 时间过期Cache。同ExpireCache不同的是,每一个元素都有自己单独的失效时间。
5. UniqueAccessExpireCache:时间过期Cache。同AccessExpireCache不同的是,每一个元素都有自己单独的失效时间。
6. ExpireLRUCache:时间过期和LRU策略的混合体。当时间过期和LRU任一过期条件被触发时,容器中的元素失效。
7. AccessExpireLRUCache:时间过期和LRU策略的混合体。同ExpireLRUCache相比,当元素被访问后,重新开始计算该元素的超时时间,而不是只从元素插入时开始计时。
8. UniqueExpireLRUCache:时间过期和LRU策略的混合体。同ExpireLRUCache相比,每一个元素都有自己单独的失效时间。
9. UniqueAccessExpireLRUCache:时间过期和LRU策略的混合体。同UniqueExpireLRUCache相比,当元素被访问后,重新开始计算该元素的超时时间,而不是只从元素插入时开始计时。
2. Cache的内部结构
2.1 Cache类
下面是Poco中Cache的类图:
从类图中我们可以看到所有的Cache都有一个对应的strategy类。事实上strategy类负责快速搜索Cache中的过期元素。Cache和strategy采用了Poco中的同步事件机制(POCO C++库学习和分析 -- 通知和事件 (四) )。
让我们来看AbstractCache的定义:
[cpp]
template
class AbstractCache
/// An AbstractCache is the interface of all caches.
{
public:
FIFOEvent, TEventMutex > Add;
FIFOEvent, TEventMutex > Update;
FIFOEvent Remove;
FIFOEvent Get;
FIFOEvent Clear;
typedef std::map > DataHolder;
typedef typename DataHolder::iterator Iterator;
typedef typename DataHolder::const_iterator ConstIterator;
typedef std::set KeySet;
AbstractCache()
{
initialize();
}
AbstractCache(const TStrategy& strat): _strategy(strat)
{
initialize();
}
virtual ~AbstractCache()
{
uninitialize();
}
// ...........
protected:
mutable FIFOEvent > IsValid;
mutable FIFOEvent Replace;
void initialize()
/// Sets up event registration.
{
Add += Delegate >(&_strategy, &TStrategy::onAdd);
Update += Delegate >(&_strategy, &TStrategy::onUpdate);
Remove += Delegate(&_strategy, &TStrategy::onRemove);
Get += Delegate(&_strategy, &TStrategy::onGet);
Clear += Delegate(&_strategy, &TStrategy::onClear);
IsValid += Delegate >(&_strategy, &TStrategy::onIsValid);
Replace += Delegate(&_strategy, &TStrategy::onReplace);
}
void uninitialize()
/// Reverts event registration.
{
Add -= Delegate >(&_strategy, &TStrategy::onAdd );
Update -= Delegate >(&_strategy, &TStrategy::onUpdate);
Remove -= Delegate(&_strategy, &TStrategy::onRemove);
Get -= Delegate(&_strategy, &TStrategy::onGet);
Clear -= Delegate(&_strategy, &TStrategy::onClear);
IsValid -= Delegate >(&_strategy, &TStrategy::onIsValid);
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇查看Objective-C函数与参数 下一篇刨根问底系列之C++ 类型转换挖掘

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目