{ CountType::SUM, "find_misses", "lru cache did not find entry in cache" },
{ CountType::SUM, "reload_prunes", "lru cache pruned entry for lower memcap during reload" },
{ CountType::SUM, "removes", "lru cache found entry and removed it" },
+ { CountType::SUM, "replaced", "lru cache found entry and replaced it" },
{ CountType::END, nullptr, nullptr },
};
PegCount find_misses = 0; // did not find entry in cache
PegCount reload_prunes = 0; // when an old entry is removed due to lower memcap during reload
PegCount removes = 0; // found entry and removed it
+ PegCount replaced = 0; // found entry and replaced it
};
template<typename Key, typename Value, typename Hash>
// Same as operator[]; additionally, sets the boolean if a new entry is created.
Data find_else_create(const Key& key, bool* new_data);
- // Returns true if found, takes a ref to a user managed entry
- bool find_else_insert(const Key& key, std::shared_ptr<Value>& data);
+ // Returns true if found or replaced, takes a ref to a user managed entry
+ bool find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace = false);
// Return all data from the LruCache in order (most recently used to least)
std::vector<std::pair<Key, Data> > get_all_data();
template<typename Key, typename Value, typename Hash>
bool LruCacheShared<Key, Value, Hash>::
-find_else_insert(const Key& key, std::shared_ptr<Value>& data)
+find_else_insert(const Key& key, std::shared_ptr<Value>& data, bool replace)
{
LruMapIter map_iter;
if (map_iter != map.end())
{
stats.find_hits++;
+ if (replace)
+ {
+ // Explicitly calling the reset so its more clear that destructor could be called for the object
+ map_iter->second->second.reset();
+ map_iter->second->second = data;
+ stats.replaced++;
+ }
list.splice(list.begin(), list, map_iter->second); // update LRU
return true;
}