[base] Lru cache: remove loader from constructor.

This commit is contained in:
tatiana-yan 2020-09-15 16:50:54 +03:00 committed by mpimenov
parent 0e71519752
commit c420036e4d
2 changed files with 18 additions and 12 deletions

View file

@ -9,16 +9,26 @@ template <typename Key, typename Value>
class LruCacheTest
{
public:
LruCacheTest(size_t maxCacheSize, typename LruCache<Key, Value>::Loader const & loader)
: m_cache(maxCacheSize, loader)
using Loader = std::function<void(Key const & key, Value & value)>;
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<Key, Value> m_cache;
Loader m_loader;
};
template <typename Key, typename Value>

View file

@ -14,25 +14,22 @@ class LruCache
template <typename K, typename V> friend class LruCacheTest;
template <typename K, typename V> friend class LruCacheKeyAgeTest;
public:
using Loader = std::function<void(Key const & key, Value & value)>;
/// \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<Key, Value> m_cache;
KeyAge m_keyAge;
};