设为首页 加入收藏

TOP

POCO C++库学习和分析 -- Cache(四)
2014-11-24 03:30:38 】 浏览:4266
Tags:POCO 学习 分析 Cache
& elemsToRemove)
{
// Note: replace only informs the cache which elements
// it would like to remove!
// it does not remove them on its own!
std::size_t curSize = _keyIndex.size();
if (curSize < _size)
{
return;
}
std::size_t diff = curSize - _size;
Iterator it = --_keys.end(); //--keys can never be invoked on an empty list due to the minSize==1 requirement of LRU
std::size_t i = 0;
while (i++ < diff)
{
elemsToRemove.insert(*it);
if (it != _keys.begin())
{
--it;
}
}
}
LRUStrategy的replace操作是,只在curSize超过设定的访问上限_size时触发,把list容器中排在末尾的(curSize-_size)个元素标志为失效。
3. 开销
Poco中的Cache类比std::map要慢,其中开销最大的操作为add操作。采用Time Expire策略的Cache要比采用LRU策略的Cache更慢。并且由于Cache类引入了SharePtr和Strategy,其空间花费也要大于std::map。所以在没有必要使用Cache的情况下,还是使用map较好。
4. 例子
下面是Cache的一个示例:
[cpp]
#include "Poco/LRUCache.h"
int main()
{
Poco::LRUCache myCache(3);
myCache.add(1, "Lousy"); // |-1-| -> first elem is the most popular one
Poco::SharedPtr ptrElem = myCache.get(1); // |-1-|
myCache.add(2, "Morning"); // |-2-1-|
myCache.add(3, "USA"); // |-3-2-1-|
// now get rid of the most unpopular entry: "Lousy"
myCache.add(4, "Good"); // |-4-3-2-|
poco_assert (*ptrElem == "Lousy"); // content of ptrElem is still valid
ptrElem = myCache.get(2); // |-2-4-3-|
// replace the morning entry with evening
myCache.add(2, "Evening"); // 2 Events: Remove followed by Add
}
首页 上一页 1 2 3 4 下一页 尾页 4/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇查看Objective-C函数与参数 下一篇刨根问底系列之C++ 类型转换挖掘

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目