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