您的位置:首页 > 编程语言 > C语言/C++

简单LRU算法实现的Cache(C++)

2013-04-07 18:26 405 查看

简单LRU算法实现的Cache(C++)

此实现不适合多线程!~没有加锁!~


#pragma once
#include <map>
#include <time.h>

template<typename CacheKey, typename CacheValue>
class LRUCache
{
public:
LRUCache(void);
LRUCache(int capacity);
~LRUCache(void);
void Put(const CacheKey & key, const CacheValue & value);
bool Get(const CacheKey & key, CacheValue & value);
void Remove(const CacheKey & key);
unsigned int Size();
private:
void removeRencentlyLeastAccess();
private:
typedef struct tagValueEntry {
CacheValue Value;
int Count;
long LastAccess;
} ValueEntry;
typedef typename std::map<CacheKey, ValueEntry> Cache;
typedef typename Cache::iterator CacheItr;
Cache m_Cache;
unsigned int m_CacheSize;
const static int DefautCacheSize = 100;
const static long MiniAccess = 20;
};
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::LRUCache(void) :
m_CacheSize(DefautCacheSize)
{
}
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::LRUCache(int capacity) :
m_CacheSize(capacity)
{
}
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::~LRUCache(void)
{
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::Put(const CacheKey & key, const CacheValue & value)
{
if(m_Cache.size() >= m_CacheSize)
{
removeRencentlyLeastAccess();
}
ValueEntry entry;
entry.Value = value;
entry.Count = 1;
entry.LastAccess = clock();
m_Cache.insert(std::make_pair(key, entry));
}
template<typename CacheKey, typename CacheValue>
bool LRUCache<CacheKey, CacheValue>::Get(const CacheKey & key, CacheValue & value)
{
CacheItr itr = m_Cache.find(key);
if(itr != m_Cache.end())
{
value = itr->second.Value;
return true;
}
return false;
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::Remove(const CacheKey & key)
{
CacheItr itr = m_Cache.find(key);
if(itr != m_Cache.end())
{
m_Cache.erase(key);
}
}
template<typename CacheKey, typename CacheValue>
unsigned int LRUCache<CacheKey, CacheValue>::Size()
{
return m_Cache.size();
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::removeRencentlyLeastAccess()
{
long earliest = 0;
const CacheKey * ToBeRemovedByTime;
int least = 0;
const CacheKey * ToBeRemovedByCount;
CacheItr itr = m_Cache.begin();
if(itr != m_Cache.end())
{
earliest = itr->second.LastAccess;
ToBeRemovedByTime = &(itr->first);
least = itr->second.Count;
ToBeRemovedByCount = &(itr->first);

for(;itr != m_Cache.end();++itr)
{
if(earliest > itr->second.LastAccess)
{
ToBeRemovedByTime = &(itr->first);
}
if(least > itr->second.Count)
{
ToBeRemovedByCount = &(itr->first);
}
}
if (least > MiniAccess) {
m_Cache.erase(*ToBeRemovedByTime);
} else {
m_Cache.erase(*ToBeRemovedByCount);
}
}
}

[cpp]
view plaincopy

#pragma once
#include <map>
#include <time.h>
template<typename CacheKey, typename CacheValue>
class LRUCache
{
public:
LRUCache(void);
LRUCache(int capacity);
~LRUCache(void);
void Put(const CacheKey & key, const CacheValue & value);
bool Get(const CacheKey & key, CacheValue & value);
void Remove(const CacheKey & key);
unsigned int Size();
private:
void removeRencentlyLeastAccess();
private:
typedef struct tagValueEntry {
CacheValue Value;
int Count;
long LastAccess;
} ValueEntry;
typedef typename std::map<CacheKey, ValueEntry> Cache;
typedef typename Cache::iterator CacheItr;
Cache m_Cache;
unsigned int m_CacheSize;
const static int DefautCacheSize = 100;
const static long MiniAccess = 20;
};
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::LRUCache(void) :
m_CacheSize(DefautCacheSize)
{
}
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::LRUCache(int capacity) :
m_CacheSize(capacity)
{
}
template<typename CacheKey, typename CacheValue>
LRUCache<CacheKey, CacheValue>::~LRUCache(void)
{
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::Put(const CacheKey & key, const CacheValue & value)
{
if(m_Cache.size() >= m_CacheSize)
{
removeRencentlyLeastAccess();
}
ValueEntry entry;
entry.Value = value;
entry.Count = 1;
entry.LastAccess = clock();
m_Cache.insert(std::make_pair(key, entry));
}
template<typename CacheKey, typename CacheValue>
bool LRUCache<CacheKey, CacheValue>::Get(const CacheKey & key, CacheValue & value)
{
CacheItr itr = m_Cache.find(key);
if(itr != m_Cache.end())
{
value = itr->second.Value;
return true;
}
return false;
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::Remove(const CacheKey & key)
{
CacheItr itr = m_Cache.find(key);
if(itr != m_Cache.end())
{
m_Cache.erase(key);
}
}
template<typename CacheKey, typename CacheValue>
unsigned int LRUCache<CacheKey, CacheValue>::Size()
{
return m_Cache.size();
}
template<typename CacheKey, typename CacheValue>
void LRUCache<CacheKey, CacheValue>::removeRencentlyLeastAccess()
{
long earliest = 0;
const CacheKey * ToBeRemovedByTime;
int least = 0;
const CacheKey * ToBeRemovedByCount;
CacheItr itr = m_Cache.begin();
if(itr != m_Cache.end())
{
earliest = itr->second.LastAccess;
ToBeRemovedByTime = &(itr->first);
least = itr->second.Count;
ToBeRemovedByCount = &(itr->first);

for(;itr != m_Cache.end();++itr)
{
if(earliest > itr->second.LastAccess)
{
ToBeRemovedByTime = &(itr->first);
}
if(least > itr->second.Count)
{
ToBeRemovedByCount = &(itr->first);
}
}
if (least > MiniAccess) {
m_Cache.erase(*ToBeRemovedByTime);
} else {
m_Cache.erase(*ToBeRemovedByCount);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: