ClickHouse/src/Common/ICachePolicy.h

43 lines
1.3 KiB
C++
Raw Normal View History

#pragma once
2022-04-30 18:00:13 +00:00
#include <functional>
#include <memory>
2022-05-14 04:44:32 +00:00
#include <mutex>
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:
using Key = TKey;
using Mapped = TMapped;
using MappedPtr = std::shared_ptr<Mapped>;
using OnWeightLossFunction = std::function<void(size_t)>;
virtual size_t weight([[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) const = 0;
virtual size_t count([[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) const = 0;
2022-05-15 03:38:44 +00:00
virtual size_t maxSize() const = 0;
virtual void reset([[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) = 0;
virtual void remove(const Key & key, [[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) = 0;
virtual MappedPtr get(const Key & key, [[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) = 0;
virtual void set(const Key & key, const MappedPtr & mapped, [[maybe_unused]] std::lock_guard<std::mutex> & cache_lock) = 0;
virtual ~ICachePolicy() = default;
protected:
OnWeightLossFunction on_weight_loss_function = [](size_t) {};
};
}