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

本文提供了一个使用C++实现的简单LRU算法的缓存,包括基本的缓存操作如添加、获取和删除。该实现不支持多线程,并且未进行加锁处理。

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

此实现不适合多线程!~没有加锁!~
  1. #pragma once   
  2. #include <map>   
  3. #include <time.h>   
  4. template<typename CacheKey, typename CacheValue>  
  5. class LRUCache  
  6. {  
  7. public:  
  8.     LRUCache(void);  
  9.     LRUCache(int capacity);  
  10.     ~LRUCache(void);  
  11.     void Put(const CacheKey & key, const CacheValue & value);  
  12.     bool Get(const CacheKey & key, CacheValue & value);  
  13.     void Remove(const CacheKey & key);  
  14.     unsigned int Size();  
  15. private:  
  16.     void removeRencentlyLeastAccess();  
  17. private:  
  18.     typedef struct tagValueEntry {  
  19.         CacheValue Value;  
  20.         int Count;  
  21.         long LastAccess;  
  22.     } ValueEntry;  
  23.     typedef typename std::map<CacheKey, ValueEntry> Cache;  
  24.     typedef typename Cache::iterator CacheItr;  
  25.     Cache m_Cache;  
  26.     unsigned int m_CacheSize;  
  27.     const static int DefautCacheSize = 100;  
  28.     const static long MiniAccess = 20;  
  29. };  
  30. template<typename CacheKey, typename CacheValue>  
  31. LRUCache<CacheKey, CacheValue>::LRUCache(void) :   
  32. m_CacheSize(DefautCacheSize)  
  33. {  
  34. }  
  35. template<typename CacheKey, typename CacheValue>  
  36. LRUCache<CacheKey, CacheValue>::LRUCache(int capacity) :   
  37. m_CacheSize(capacity)  
  38. {  
  39. }  
  40. template<typename CacheKey, typename CacheValue>  
  41. LRUCache<CacheKey, CacheValue>::~LRUCache(void)  
  42. {  
  43. }  
  44. template<typename CacheKey, typename CacheValue>  
  45. void LRUCache<CacheKey, CacheValue>::Put(const CacheKey & key, const CacheValue & value)  
  46. {  
  47.     if(m_Cache.size() >= m_CacheSize)  
  48.     {  
  49.         removeRencentlyLeastAccess();  
  50.     }  
  51.     ValueEntry entry;  
  52.     entry.Value = value;  
  53.     entry.Count = 1;  
  54.     entry.LastAccess = clock();  
  55.     m_Cache.insert(std::make_pair(key, entry));  
  56. }  
  57. template<typename CacheKey, typename CacheValue>  
  58. bool LRUCache<CacheKey, CacheValue>::Get(const CacheKey & key, CacheValue & value)  
  59. {  
  60.     CacheItr itr = m_Cache.find(key);  
  61.     if(itr != m_Cache.end())  
  62.     {  
  63.         value = itr->second.Value;  
  64.         return true;  
  65.     }  
  66.     return false;  
  67. }  
  68. template<typename CacheKey, typename CacheValue>  
  69. void LRUCache<CacheKey, CacheValue>::Remove(const CacheKey & key)  
  70. {  
  71.     CacheItr itr = m_Cache.find(key);  
  72.     if(itr != m_Cache.end())  
  73.     {  
  74.         m_Cache.erase(key);  
  75.     }  
  76. }  
  77. template<typename CacheKey, typename CacheValue>  
  78. unsigned int LRUCache<CacheKey, CacheValue>::Size()  
  79. {  
  80.     return m_Cache.size();  
  81. }  
  82. template<typename CacheKey, typename CacheValue>  
  83. void LRUCache<CacheKey, CacheValue>::removeRencentlyLeastAccess()  
  84. {  
  85.     long earliest = 0;  
  86.     const CacheKey * ToBeRemovedByTime;  
  87.     int least = 0;  
  88.     const CacheKey * ToBeRemovedByCount;  
  89.     CacheItr itr = m_Cache.begin();  
  90.     if(itr != m_Cache.end())  
  91.     {  
  92.         earliest = itr->second.LastAccess;  
  93.         ToBeRemovedByTime = &(itr->first);  
  94.         least = itr->second.Count;  
  95.         ToBeRemovedByCount = &(itr->first);  
  96.           
  97.         for(;itr != m_Cache.end();++itr)  
  98.         {  
  99.             if(earliest > itr->second.LastAccess)  
  100.             {  
  101.                 ToBeRemovedByTime = &(itr->first);  
  102.             }  
  103.             if(least > itr->second.Count)  
  104.             {  
  105.                 ToBeRemovedByCount = &(itr->first);  
  106.             }  
  107.         }  
  108.         if (least > MiniAccess) {  
  109.             m_Cache.erase(*ToBeRemovedByTime);  
  110.         } else {  
  111.             m_Cache.erase(*ToBeRemovedByCount);  
  112.         }  
  113.     }  
  114. }  
  1. #pragma once  
  2. #include <map>  
  3. #include <time.h>  
  4. template<typename CacheKey, typename CacheValue>  
  5. class LRUCache  
  6. {  
  7. public:  
  8.     LRUCache(void);  
  9.     LRUCache(int capacity);  
  10.     ~LRUCache(void);  
  11.     void Put(const CacheKey & key, const CacheValue & value);  
  12.     bool Get(const CacheKey & key, CacheValue & value);  
  13.     void Remove(const CacheKey & key);  
  14.     unsigned int Size();  
  15. private:  
  16.     void removeRencentlyLeastAccess();  
  17. private:  
  18.     typedef struct tagValueEntry {  
  19.         CacheValue Value;  
  20.         int Count;  
  21.         long LastAccess;  
  22.     } ValueEntry;  
  23.     typedef typename std::map<CacheKey, ValueEntry> Cache;  
  24.     typedef typename Cache::iterator CacheItr;  
  25.     Cache m_Cache;  
  26.     unsigned int m_CacheSize;  
  27.     const static int DefautCacheSize = 100;  
  28.     const static long MiniAccess = 20;  
  29. };  
  30. template<typename CacheKey, typename CacheValue>  
  31. LRUCache<CacheKey, CacheValue>::LRUCache(void) :   
  32. m_CacheSize(DefautCacheSize)  
  33. {  
  34. }  
  35. template<typename CacheKey, typename CacheValue>  
  36. LRUCache<CacheKey, CacheValue>::LRUCache(int capacity) :   
  37. m_CacheSize(capacity)  
  38. {  
  39. }  
  40. template<typename CacheKey, typename CacheValue>  
  41. LRUCache<CacheKey, CacheValue>::~LRUCache(void)  
  42. {  
  43. }  
  44. template<typename CacheKey, typename CacheValue>  
  45. void LRUCache<CacheKey, CacheValue>::Put(const CacheKey & key, const CacheValue & value)  
  46. {  
  47.     if(m_Cache.size() >= m_CacheSize)  
  48.     {  
  49.         removeRencentlyLeastAccess();  
  50.     }  
  51.     ValueEntry entry;  
  52.     entry.Value = value;  
  53.     entry.Count = 1;  
  54.     entry.LastAccess = clock();  
  55.     m_Cache.insert(std::make_pair(key, entry));  
  56. }  
  57. template<typename CacheKey, typename CacheValue>  
  58. bool LRUCache<CacheKey, CacheValue>::Get(const CacheKey & key, CacheValue & value)  
  59. {  
  60.     CacheItr itr = m_Cache.find(key);  
  61.     if(itr != m_Cache.end())  
  62.     {  
  63.         value = itr->second.Value;  
  64.         return true;  
  65.     }  
  66.     return false;  
  67. }  
  68. template<typename CacheKey, typename CacheValue>  
  69. void LRUCache<CacheKey, CacheValue>::Remove(const CacheKey & key)  
  70. {  
  71.     CacheItr itr = m_Cache.find(key);  
  72.     if(itr != m_Cache.end())  
  73.     {  
  74.         m_Cache.erase(key);  
  75.     }  
  76. }  
  77. template<typename CacheKey, typename CacheValue>  
  78. unsigned int LRUCache<CacheKey, CacheValue>::Size()  
  79. {  
  80.     return m_Cache.size();  
  81. }  
  82. template<typename CacheKey, typename CacheValue>  
  83. void LRUCache<CacheKey, CacheValue>::removeRencentlyLeastAccess()  
  84. {  
  85.     long earliest = 0;  
  86.     const CacheKey * ToBeRemovedByTime;  
  87.     int least = 0;  
  88.     const CacheKey * ToBeRemovedByCount;  
  89.     CacheItr itr = m_Cache.begin();  
  90.     if(itr != m_Cache.end())  
  91.     {  
  92.         earliest = itr->second.LastAccess;  
  93.         ToBeRemovedByTime = &(itr->first);  
  94.         least = itr->second.Count;  
  95.         ToBeRemovedByCount = &(itr->first);  
  96.           
  97.         for(;itr != m_Cache.end();++itr)  
  98.         {  
  99.             if(earliest > itr->second.LastAccess)  
  100.             {  
  101.                 ToBeRemovedByTime = &(itr->first);  
  102.             }  
  103.             if(least > itr->second.Count)  
  104.             {  
  105.                 ToBeRemovedByCount = &(itr->first);  
  106.             }  
  107.         }  
  108.         if (least > MiniAccess) {  
  109.             m_Cache.erase(*ToBeRemovedByTime);  
  110.         } else {  
  111.             m_Cache.erase(*ToBeRemovedByCount);  
  112.         }  
  113.     }  
  114. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值