LRUCache fix exception unsafe element insertion

This commit is contained in:
Maksim Kita 2021-03-19 00:26:02 +03:00
parent f1223e7548
commit 31b7734407

View File

@ -271,17 +271,24 @@ private:
void setImpl(const Key & key, const MappedPtr & mapped, [[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) void setImpl(const Key & key, const MappedPtr & mapped, [[maybe_unused]] std::lock_guard<std::mutex> & cache_lock)
{ {
auto res = cells.emplace(std::piecewise_construct, auto [it, inserted] = cells.emplace(std::piecewise_construct,
std::forward_as_tuple(key), std::forward_as_tuple(key),
std::forward_as_tuple()); std::forward_as_tuple());
Cell & cell = res.first->second; Cell & cell = it->second;
bool inserted = res.second;
if (inserted) if (inserted)
{
try
{ {
cell.queue_iterator = queue.insert(queue.end(), key); cell.queue_iterator = queue.insert(queue.end(), key);
} }
catch (...)
{
cells.erase(it);
throw;
}
}
else else
{ {
current_size -= cell.size; current_size -= cell.size;