Caches 0.1.0
LRU/LFU/FIFO Caches library
Loading...
Searching...
No Matches
cache_policy.hpp
Go to the documentation of this file.
1
5#ifndef CACHE_POLICY_HPP
6#define CACHE_POLICY_HPP
7
8#include <unordered_set>
9
10namespace caches
11{
12
17template <typename Key>
19{
20 public:
21 virtual ~ICachePolicy() = default;
22
27 virtual void Insert(const Key &key) = 0;
28
33 virtual void Touch(const Key &key) = 0;
38 virtual void Erase(const Key &key) = 0;
39
44 virtual const Key &ReplCandidate() const = 0;
45};
46
55template <typename Key>
56class NoCachePolicy : public ICachePolicy<Key>
57{
58 public:
59 NoCachePolicy() = default;
60 ~NoCachePolicy() noexcept override = default;
61
62 void Insert(const Key &key) override
63 {
64 key_storage.emplace(key);
65 }
66
67 void Touch(const Key &key) noexcept override
68 {
69 // do not do anything
70 (void)key;
71 }
72
73 void Erase(const Key &key) noexcept override
74 {
75 key_storage.erase(key);
76 }
77
78 // return a key of a displacement candidate
79 const Key &ReplCandidate() const noexcept override
80 {
81 return *key_storage.cbegin();
82 }
83
84 private:
85 std::unordered_set<Key> key_storage;
86};
87} // namespace caches
88
89#endif // CACHE_POLICY_HPP
Cache policy abstract base class.
Definition: cache_policy.hpp:19
virtual void Erase(const Key &key)=0
Handle element deletion from a cache.
virtual const Key & ReplCandidate() const =0
Return a key of a replacement candidate.
virtual void Insert(const Key &key)=0
Handle element insertion in a cache.
virtual void Touch(const Key &key)=0
Handle request to the key-element in a cache.
Basic no caching policy class.
Definition: cache_policy.hpp:57
void Touch(const Key &key) noexcept override
Handle request to the key-element in a cache.
Definition: cache_policy.hpp:67
const Key & ReplCandidate() const noexcept override
Return a key of a replacement candidate.
Definition: cache_policy.hpp:79
void Insert(const Key &key) override
Handle element insertion in a cache.
Definition: cache_policy.hpp:62
void Erase(const Key &key) noexcept override
Handle element deletion from a cache.
Definition: cache_policy.hpp:73