Caches 0.1.0
LRU/LFU/FIFO Caches library
Loading...
Searching...
No Matches
fifo_cache_policy.hpp
Go to the documentation of this file.
1
5#ifndef FIFO_CACHE_POLICY_HPP
6#define FIFO_CACHE_POLICY_HPP
7
8#include "cache_policy.hpp"
9#include <list>
10#include <unordered_map>
11
12namespace caches
13{
14
34template <typename Key>
35class FIFOCachePolicy : public ICachePolicy<Key>
36{
37 public:
38 using fifo_iterator = typename std::list<Key>::const_iterator;
39
40 FIFOCachePolicy() = default;
41 ~FIFOCachePolicy() = default;
42
43 void Insert(const Key &key) override
44 {
45 fifo_queue.emplace_front(key);
46 key_lookup[key] = fifo_queue.begin();
47 }
48 // handle request to the key-element in a cache
49 void Touch(const Key &key) noexcept override
50 {
51 // nothing to do here in the FIFO strategy
52 (void)key;
53 }
54 // handle element deletion from a cache
55 void Erase(const Key &key) noexcept override
56 {
57 auto element = key_lookup[key];
58 fifo_queue.erase(element);
59 key_lookup.erase(key);
60 }
61
62 // return a key of a replacement candidate
63 const Key &ReplCandidate() const noexcept override
64 {
65 return fifo_queue.back();
66 }
67
68 private:
69 std::list<Key> fifo_queue;
70 std::unordered_map<Key, fifo_iterator> key_lookup;
71};
72} // namespace caches
73
74#endif // FIFO_CACHE_POLICY_HPP
Cache policy interface declaration.
FIFO (First in, first out) cache policy.
Definition: fifo_cache_policy.hpp:36
void Insert(const Key &key) override
Handle element insertion in a cache.
Definition: fifo_cache_policy.hpp:43
void Touch(const Key &key) noexcept override
Handle request to the key-element in a cache.
Definition: fifo_cache_policy.hpp:49
void Erase(const Key &key) noexcept override
Handle element deletion from a cache.
Definition: fifo_cache_policy.hpp:55
const Key & ReplCandidate() const noexcept override
Return a key of a replacement candidate.
Definition: fifo_cache_policy.hpp:63
Cache policy abstract base class.
Definition: cache_policy.hpp:19