ClickHouse/src/Common/ICachePolicy.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

43 lines
1.2 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)>;
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-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;
virtual ~ICachePolicy() = default;
protected:
OnWeightLossFunction on_weight_loss_function = [](size_t) {};
};
}