2022-04-28 05:56:26 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-04-30 18:00:13 +00:00
|
|
|
#include <functional>
|
2022-04-28 05:56:26 +00:00
|
|
|
#include <memory>
|
2022-05-14 04:44:32 +00:00
|
|
|
#include <mutex>
|
2022-04-28 05:56:26 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
template <typename T>
|
|
|
|
struct TrivialWeightFunction
|
|
|
|
{
|
|
|
|
size_t operator()(const T &) const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TKey, typename TMapped, typename HashFunction = std::hash<TKey>, typename WeightFunction = TrivialWeightFunction<TMapped>>
|
|
|
|
class ICachePolicy
|
|
|
|
{
|
2022-04-28 07:48:15 +00:00
|
|
|
public:
|
2022-04-28 05:56:26 +00:00
|
|
|
using Key = TKey;
|
|
|
|
using Mapped = TMapped;
|
|
|
|
using MappedPtr = std::shared_ptr<Mapped>;
|
|
|
|
using OnWeightLossFunction = std::function<void(size_t)>;
|
|
|
|
|
2022-08-07 17:16:07 +00:00
|
|
|
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;
|
2022-05-15 03:38:44 +00:00
|
|
|
virtual size_t maxSize() const = 0;
|
2022-05-14 04:15:57 +00:00
|
|
|
|
2022-08-07 17:16:07 +00:00
|
|
|
virtual void reset(std::lock_guard<std::mutex> & /* cache_lock */) = 0;
|
|
|
|
virtual void remove(const Key & key, std::lock_guard<std::mutex> & /* cache_lock */) = 0;
|
|
|
|
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;
|
2022-04-28 05:56:26 +00:00
|
|
|
|
|
|
|
virtual ~ICachePolicy() = default;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
OnWeightLossFunction on_weight_loss_function = [](size_t) {};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|