From cc9e39825c853ff80014d20013142e8926a99f5f Mon Sep 17 00:00:00 2001 From: sowle Date: Wed, 21 May 2025 04:33:24 +0300 Subject: [PATCH] cache_helper/cache_base fixed and improved --- contrib/epee/include/cache_helper.h | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/contrib/epee/include/cache_helper.h b/contrib/epee/include/cache_helper.h index 416b58bd..7d42fe73 100644 --- a/contrib/epee/include/cache_helper.h +++ b/contrib/epee/include/cache_helper.h @@ -98,7 +98,11 @@ namespace epee if (it == data.end()) return false; - most_recet_acessed.splice(most_recet_acessed.begin(), most_recet_acessed, it->second.second); + //most_recet_acessed.splice(most_recet_acessed.begin(), most_recet_acessed, it->second.second); + most_recet_acessed.erase(it->second.second); + most_recet_acessed.push_front(k); + it->second.second = most_recet_acessed.begin(); + v = it->second.first; return true; } @@ -106,8 +110,18 @@ namespace epee bool set(const t_key& k, const t_value& v) { CRITICAL_REGION_LOCAL(m_lock); - most_recet_acessed.push_front(k); - data[k] = std::pair::iterator>(v, most_recet_acessed.begin()); + auto it = data.find(k); + if (it == data.end()) + { + most_recet_acessed.push_front(k); + data.insert(std::make_pair(k, std::make_pair(v, most_recet_acessed.begin()))); + } + else + { + most_recet_acessed.erase(it->second.second); + most_recet_acessed.push_front(k); + it->second.second = most_recet_acessed.begin(); + } trim(); return true; @@ -248,7 +262,10 @@ namespace epee bool erase(const t_key& k) { - return m_isolation.isolated_write_access([&](){return base_class::erase(k); }); + return m_isolation.isolated_write_access([&]() + { + return base_class::erase(k); + }); } };