简单LRU算法实现的Cache(C++)
来源:互联网 发布:网络视听许可 编辑:程序博客网 时间:2024/06/07 18:14
简单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);
- }
- }
- }
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- 简单LRU算法实现的Cache(C++)
- LRU cache的简单实现
- LRU Cache的简单实现
- LRU cache 算法的实现
- 简单LRU cache 实现
- 用LinkedHashMap实现的简单LRU Cache
- LRU Cache的简单c++实现
- LRU cache的一个简单实现
- java实现简单的LRU Cache
- LRU Cache算法实现(leetcode)
- Java实现简单的LRU缓存(A Simple LRU Cache in 5 lines)
- Java实现简单的LRU缓存(A Simple LRU Cache in 5 lines)
- LRU cache的实现
- LRU Cache的实现
- LRU Cache的实现
- Sass 技术和相关工具介绍
- word目录对齐方法
- Openstack组件介绍
- TCP/IP详解学习笔记(5)-IP选路,动态选路,和一些细节
- ASIHttpRequest-创建和执行request 的 异步加载和使用块
- 简单LRU算法实现的Cache(C++)
- 全排列生成算法 .
- spfile和pfile
- 涉足计算机视觉领域要知道的
- 佳能(中国)宠物题材的拍摄效果例和要领介绍。
- Regular Expressions
- memcache的最佳实践方案
- 一步步学习Reporting Services(一) 创建第一张报表
- asp.net中DataList的用法