5#ifndef LRU_CACHE_POLICY_HPP
6#define LRU_CACHE_POLICY_HPP
10#include <unordered_map>
36template <
typename Key>
40 using lru_iterator =
typename std::list<Key>::iterator;
47 lru_queue.emplace_front(key);
48 key_finder[key] = lru_queue.begin();
51 void Touch(
const Key &key)
override
54 lru_queue.splice(lru_queue.begin(), lru_queue, key_finder[key]);
57 void Erase(
const Key &)
noexcept override
60 key_finder.erase(lru_queue.back());
67 return lru_queue.back();
71 std::list<Key> lru_queue;
72 std::unordered_map<Key, lru_iterator> key_finder;
Cache policy interface declaration.
Cache policy abstract base class.
Definition: cache_policy.hpp:19
LRU (Least Recently Used) cache policy.
Definition: lru_cache_policy.hpp:38
void Touch(const Key &key) override
Handle request to the key-element in a cache.
Definition: lru_cache_policy.hpp:51
const Key & ReplCandidate() const noexcept override
Return a key of a replacement candidate.
Definition: lru_cache_policy.hpp:65
void Erase(const Key &) noexcept override
Handle element deletion from a cache.
Definition: lru_cache_policy.hpp:57
void Insert(const Key &key) override
Handle element insertion in a cache.
Definition: lru_cache_policy.hpp:45