Skip to content
Closed
15 changes: 7 additions & 8 deletions src/concurrent_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ namespace zim
available.
*/
template <typename Key, typename Value>
class ConcurrentCache
class ConcurrentCache: private lru_cache<Key, std::shared_future<Value>>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The explanation in the commit description is not a good one. Private inheritance is not an "is a" relation. To the best of my understanding, this change is made so that ConcurrentCache may have access to the protected API of lru_cache.

{
private: // types
typedef std::shared_future<Value> ValuePlaceholder;
typedef lru_cache<Key, ValuePlaceholder> Impl;

public: // types
explicit ConcurrentCache(size_t maxEntries)
: impl_(maxEntries)
: Impl(maxEntries)
{}

// Gets the entry corresponding to the given key. If the entry is not in the
Expand All @@ -65,7 +65,7 @@ class ConcurrentCache
{
std::promise<Value> valuePromise;
std::unique_lock<std::mutex> l(lock_);
const auto x = impl_.getOrPut(key, valuePromise.get_future().share());
const auto x = Impl::getOrPut(key, valuePromise.get_future().share());
l.unlock();
if ( x.miss() ) {
try {
Expand All @@ -82,26 +82,25 @@ class ConcurrentCache
bool drop(const Key& key)
{
std::unique_lock<std::mutex> l(lock_);
return impl_.drop(key);
return Impl::drop(key);
}

size_t getMaxSize() const {
std::unique_lock<std::mutex> l(lock_);
return impl_.getMaxSize();
return Impl::getMaxSize();
}

size_t getCurrentSize() const {
std::unique_lock<std::mutex> l(lock_);
return impl_.size();
return Impl::size();
}

void setMaxSize(size_t newSize) {
std::unique_lock<std::mutex> l(lock_);
return impl_.setMaxSize(newSize);
return Impl::setMaxSize(newSize);
}

private: // data
Impl impl_;
mutable std::mutex lock_;
};

Expand Down