Files
Rediska/rediska/cache/lru/LRU.hpp
Nikita Astafyev 35a6349071
Some checks failed
Code Analysis / analysis (push) Failing after 2m59s
chore: initial commit
2025-12-30 22:34:58 +07:00

53 lines
1.4 KiB
C++

#pragma once
#include <list>
#include <shared_mutex>
#include <unordered_map>
#include "rediska/cache/lru/LRUConfig.hpp"
#include "rediska/cache/lru/LRUItemMetadata.hpp"
#include "rediska/cache/types.hpp"
#include "rediska/common/MessageArguments.hpp"
#include "rediska/common/types.hpp"
#include "rediska/cache/CachePolicy.hpp"
namespace cache {
class LRU : public CachePolicy {
public:
using ItemMetadata = LRUItemMetadata;
using ItemHandle = struct {
CacheValue value;
ItemMetadata metadata;
};
using CacheNode = struct {
CacheKey key;
ItemHandle location;
};
LRU(LRUConfig config, CacheOpCallback callback);
~LRU() = default;
void get(CacheKey&& key) override;
void set(CacheKey&& key, CacheValue&& value, TTL ttl) override;
void applyTo(CacheKey&& key, OperationId op, MessageArguments&& args) override;
private:
std::list<CacheNode> lru_list_;
std::unordered_map<CacheKey, std::list<CacheNode>::iterator> keyToItem_;
std::shared_mutex mutex_;
LRUConfig config_;
CacheOpCallback callback_;
void evict() override;
void evict(const std::unordered_map<CacheKey, std::list<CacheNode>::iterator>::iterator node);
inline bool isFull() const override;
void resetTTLIfEnabled(CacheNode& key);
};
}