简单LRU算法实现的Cache(C++)
2012-10-23 20:44
621 查看
print?
#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);
}
}
}
#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);
}
}
}
此实现不适合多线程!~没有加锁!~
#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);
}
}
}
#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);
}
}
}
此实现不适合多线程!~没有加锁!~
相关文章推荐
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- C++ 命令输入简单算法的实现
- C++实现简单遗传算法
- Java实现简单的LRU缓存(A Simple LRU Cache in 5 lines)
- 使用LinkedHashMap来实现一个使用LRU(Least Recently Used)算法的cache
- 最简单的LRU算法java实现
- 使用C++实现一个LRU cache
- 简单LRU算法实现缓存
- c++实现的常见缓存算法和LRU
- 简单插入算法的C++实现
- 算法导论 4.1-5 最大子数组问题 线性时间算法 C++简单实现
- 简单的学生信息处理程序实现 (Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;OpenJudge)
- 简单LRU算法实现缓存
- 简单LRU算法实现缓存
- 算法与数据结构基础6:C++队列的简单实现
- 实现一个简单的c++ list容器(含sort排序 链表归并算法实现)
- Java实现简单的LRU缓存(A Simple LRU Cache in 5 lines)