Extend CacheBase with dump() method

This commit is contained in:
Robert Schulze 2023-03-13 15:28:03 +00:00
parent e03618357e
commit f1450923cb
No known key found for this signature in database
GPG Key ID: 26703B55FB13728A
4 changed files with 33 additions and 0 deletions

View File

@ -37,6 +37,7 @@ public:
using Key = typename CachePolicy::Key;
using Mapped = typename CachePolicy::Mapped;
using MappedPtr = typename CachePolicy::MappedPtr;
using KeyMapped = typename CachePolicy::KeyMapped;
/// Use this ctor if you don't care about the internal cache policy.
explicit CacheBase(size_t max_size_in_bytes, size_t max_entries = 0, double size_ratio = 0.5)
@ -160,6 +161,12 @@ public:
out_misses = misses;
}
std::vector<KeyMapped> dump() const
{
std::lock_guard lock(mutex);
return cache_policy->dump();
}
void reset()
{
std::lock_guard lock(mutex);

View File

@ -25,6 +25,12 @@ public:
using MappedPtr = std::shared_ptr<Mapped>;
using OnWeightLossFunction = std::function<void(size_t)>;
struct KeyMapped
{
Key key;
MappedPtr mapped;
};
virtual size_t weight(std::lock_guard<std::mutex> & /* cache_lock */) const = 0;
virtual size_t count(std::lock_guard<std::mutex> & /* cache_lock */) const = 0;
virtual size_t maxSize(std::lock_guard<std::mutex>& /* cache_lock */) const = 0;
@ -34,6 +40,8 @@ public:
virtual MappedPtr get(const Key & key, std::lock_guard<std::mutex> & /* cache_lock */) = 0;
virtual void set(const Key & key, const MappedPtr & mapped, std::lock_guard<std::mutex> & /* cache_lock */) = 0;
virtual std::vector<KeyMapped> dump() const = 0;
virtual ~ICachePolicy() = default;
};

View File

@ -22,6 +22,7 @@ class LRUCachePolicy : public ICachePolicy<Key, Mapped, HashFunction, WeightFunc
public:
using Base = ICachePolicy<Key, Mapped, HashFunction, WeightFunction>;
using typename Base::MappedPtr;
using typename Base::KeyMapped;
using typename Base::OnWeightLossFunction;
/** Initialize LRUCachePolicy with max_size_in_bytes and max_entries.
@ -116,6 +117,14 @@ public:
removeOverflow();
}
std::vector<KeyMapped> dump() const override
{
std::vector<KeyMapped> res;
for (const auto & [key, cell] : cells)
res.push_back({key, cell.value});
return res;
}
protected:
using LRUQueue = std::list<Key>;
using LRUQueueIterator = typename LRUQueue::iterator;

View File

@ -24,6 +24,7 @@ class SLRUCachePolicy : public ICachePolicy<Key, Mapped, HashFunction, WeightFun
public:
using Base = ICachePolicy<Key, Mapped, HashFunction, WeightFunction>;
using typename Base::MappedPtr;
using typename Base::KeyMapped;
using typename Base::OnWeightLossFunction;
/** Initialize SLRUCachePolicy with max_size_in_bytes and max_protected_size.
@ -148,6 +149,14 @@ public:
removeOverflow(probationary_queue, max_size_in_bytes, current_size_in_bytes, /*is_protected=*/false);
}
std::vector<KeyMapped> dump() const override
{
std::vector<KeyMapped> res;
for (const auto & [key, cell] : cells)
res.push_back({key, cell.value});
return res;
}
protected:
using SLRUQueue = std::list<Key>;
using SLRUQueueIterator = typename SLRUQueue::iterator;