65 unsigned int d_max_items = 100;
66 unsigned int d_purge_items = 20;
68 std::deque<std::string> d_fifo_keys;
69 std::unordered_map<std::string, VALUE> d_cache;
74 for (
int entries = 0; entries < d_purge_items; ++entries) {
75 std::string key = d_fifo_keys.front();
77 d_cache.erase(d_cache.find(key));
78 d_fifo_keys.pop_front();
83 bool invariant(
bool expensive =
true)
const {
84 if (d_cache.size() > d_max_items)
86 if (d_fifo_keys.size() > d_max_items)
88 if (d_cache.size() != d_fifo_keys.size())
93 for (
const auto &key : d_fifo_keys) {
94 if (d_cache.find(key) == d_cache.end())
102 friend class MemoryCacheTest;
105 MemoryCache() =
default;
106 MemoryCache(
const MemoryCache *src) =
delete;
108 virtual ~MemoryCache() =
default;
109 MemoryCache &operator=(
const MemoryCache *src) =
delete;
118 if (max_items <= 0 || purge_items <= 0)
121 d_max_items = (
unsigned int)max_items;
122 d_purge_items = (
unsigned int)purge_items;
133 virtual bool get(
const std::string &key, VALUE &value) {
134 if (d_cache.find(key) != d_cache.end()) {
135 value = d_cache[key];
149 virtual void put(
const std::string &key,
const VALUE &value) {
152 if (d_cache.find(key) == d_cache.end()) {
153 d_cache.insert(std::pair<std::string, VALUE>(key, value));
154 d_fifo_keys.push_back(key);
155 if (d_cache.size() > d_max_items)
159 d_cache[key] = value;
163 virtual unsigned long size()
const {
return d_cache.size(); }
166 virtual void clear() { d_cache.clear(); d_fifo_keys.clear(); }
virtual bool get(const std::string &key, VALUE &value)
Get the item from the cache. If the item is not in the cache, the value-result parameter is not modif...