diff --git a/base/base_tests/lru_cache_test.cpp b/base/base_tests/lru_cache_test.cpp index 3eaae90cd0..b49ee1a912 100644 --- a/base/base_tests/lru_cache_test.cpp +++ b/base/base_tests/lru_cache_test.cpp @@ -9,16 +9,26 @@ template class LruCacheTest { public: - LruCacheTest(size_t maxCacheSize, typename LruCache::Loader const & loader) - : m_cache(maxCacheSize, loader) + using Loader = std::function; + + LruCacheTest(size_t maxCacheSize, Loader const & loader) : m_cache(maxCacheSize), m_loader(loader) { } - Value const & GetValue(Key const & key) { return m_cache.GetValue(key); } + Value const & GetValue(Key const & key) + { + bool found; + auto & value = m_cache.Find(key, found); + if (!found) + m_loader(key, value); + return value; + } + bool IsValid() const { return m_cache.IsValid(); } private: LruCache m_cache; + Loader m_loader; }; template diff --git a/base/lru_cache.hpp b/base/lru_cache.hpp index e9bdcaf73b..bacd719527 100644 --- a/base/lru_cache.hpp +++ b/base/lru_cache.hpp @@ -14,25 +14,22 @@ class LruCache template friend class LruCacheTest; template friend class LruCacheKeyAgeTest; public: - using Loader = std::function; - /// \param maxCacheSize Maximum size of the cache in number of items. It should be one or greater. /// \param loader Function which is called if it's necessary to load a new item for the cache. /// For the same |key| should be loaded the same |value|. - LruCache(size_t maxCacheSize, Loader const & loader) - : m_maxCacheSize(maxCacheSize), m_loader(loader) + LruCache(size_t maxCacheSize) : m_maxCacheSize(maxCacheSize) { CHECK_GREATER(maxCacheSize, 0, ()); } - /// \brief Loads value, if it's necessary, by |key| with |m_loader|, puts it to |m_cache| and - /// returns the reference to the value to |m_cache|. - Value const & GetValue(Key const & key) + // Find value by @key. If @key is found, returns reference to its value. + Value & Find(Key const & key, bool & found) { auto const it = m_cache.find(key); if (it != m_cache.cend()) { m_keyAge.UpdateAge(key); + found = true; return it->second; } @@ -44,7 +41,7 @@ public: m_keyAge.InsertKey(key); Value & value = m_cache[key]; - m_loader(key, value); + found = false; return value; } @@ -158,7 +155,6 @@ private: }; size_t const m_maxCacheSize; - Loader const m_loader; std::unordered_map m_cache; KeyAge m_keyAge; };