简单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);
		}
	}
}

原文地址:

http://blog.csdn.net/huyiyang2010/article/details/5827623

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值