| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <functional> |
| 4 |
#include <algorithm> |
| 5 |
#include <map> |
| 6 |
#include <limits> |
| 7 |
#include <utility> |
| 8 |
#include <mutex> |
| 9 |
#include <memory> |
| 10 |
|
| 11 |
|
| 12 |
namespace Davix { |
| 13 |
|
| 14 |
/// |
| 15 |
/// Thread Safe Cache container |
| 16 |
/// |
| 17 |
template <class Key, class Value, class CompareT = std::less<Key> > |
| 18 |
class Cache { |
| 19 |
typedef std::map<Key, std::shared_ptr<Value> > Map; |
| 20 |
typedef std::shared_ptr<Value> shrPtr_type; |
| 21 |
|
| 22 |
public: |
| 23 |
Cache(size_t max_size = std::numeric_limits<size_t>::max()) : |
| 24 |
cmp(CompareT()), map(cmp), _max_size(max_size), _m(){} |
| 25 |
|
| 26 |
~Cache(){} |
| 27 |
|
| 28 |
shrPtr_type insert(const Key & key, Value* value){ |
| 29 |
return insert(key, shrPtr_type(value)); |
| 30 |
} |
| 31 |
|
| 32 |
/// |
| 33 |
/// \brief insert a new value into the cache, if an already existing one is mapped to this case, the old value is replaced |
| 34 |
/// \param key |
| 35 |
/// \param value |
| 36 |
/// \return |
| 37 |
/// |
| 38 |
shrPtr_type insert(const Key & key, const shrPtr_type & value){ |
| 39 |
std::lock_guard<std::mutex> l(_m); |
| 40 |
auto_clean(); |
| 41 |
std::pair< typename Map::iterator, bool > stored = map.insert(std::pair<Key,shrPtr_type> (key, value) ); |
| 42 |
if(stored.second) |
| 43 |
return (*stored.first).second; |
| 44 |
(*stored.first).second = value; |
| 45 |
return value; |
| 46 |
} |
| 47 |
|
| 48 |
/// |
| 49 |
/// \brief find a cached value, returned a NULL shared_pointer if not found |
| 50 |
/// \param key |
| 51 |
/// \return |
| 52 |
/// |
| 53 |
shrPtr_type find(const Key & key){ |
| 54 |
std::lock_guard<std::mutex> l(_m); |
| 55 |
typename Map::iterator it = map.find( key); |
| 56 |
if(it == map.end()) |
| 57 |
return shrPtr_type(); |
| 58 |
return it->second; |
| 59 |
} |
| 60 |
|
| 61 |
/// |
| 62 |
/// \brief return the key that is immediatelly after given key |
| 63 |
/// \param key |
| 64 |
/// \return the next key |
| 65 |
/// |
| 66 |
const Key upper_bound(const Key & key) { |
| 67 |
std::lock_guard<std::mutex> l(_m); |
| 68 |
typename Map::iterator it = map.upper_bound( key); |
| 69 |
if(it == map.end()) |
| 70 |
return Key(); |
| 71 |
return it->first; |
| 72 |
} |
| 73 |
|
| 74 |
/// |
| 75 |
/// \brief getSize return the number of object sotred in the cache |
| 76 |
/// \return |
| 77 |
/// |
| 78 |
size_t getSize() const{ |
| 79 |
std::lock_guard<std::mutex> l(_m); |
| 80 |
return map.size(); |
| 81 |
} |
| 82 |
|
| 83 |
/// |
| 84 |
/// \brief remove an object from this cache and return its reference |
| 85 |
/// \param key |
| 86 |
/// \return |
| 87 |
/// |
| 88 |
shrPtr_type take( const Key & key){ |
| 89 |
std::lock_guard<std::mutex> l(_m); |
| 90 |
typename Map::iterator it = map.find( key); |
| 91 |
if(it == map.end()) return shrPtr_type(); |
| 92 |
|
| 93 |
shrPtr_type ret = it->second; |
| 94 |
map.erase(it); |
| 95 |
return ret; |
| 96 |
} |
| 97 |
|
| 98 |
/// |
| 99 |
/// \brief erase an object from the cache |
| 100 |
/// \param key |
| 101 |
/// \return |
| 102 |
/// |
| 103 |
bool erase( const Key & key){ |
| 104 |
std::lock_guard<std::mutex> l(_m); |
| 105 |
return ( map.erase(key) != 0); |
| 106 |
} |
| 107 |
|
| 108 |
void clear(){ |
| 109 |
std::lock_guard<std::mutex> l(_m); |
| 110 |
map.clear(); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
protected: |
| 115 |
CompareT cmp; |
| 116 |
Map map; |
| 117 |
size_t _max_size; |
| 118 |
mutable std::mutex _m; |
| 119 |
|
| 120 |
void auto_clean(){ |
| 121 |
if(map.size() == _max_size){ |
| 122 |
map.clear(); |
| 123 |
} |
| 124 |
} |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
} |