Electroneum
cache_policy.hpp
Go to the documentation of this file.
1 #ifndef CACHE_POLICY_HPP
2 #define CACHE_POLICY_HPP
3 
4 #include <unordered_set>
5 
6 namespace caches
7 {
8 
9 template <typename Key>
10 
12 {
13  public:
14 
15  virtual ~ICachePolicy() {}
16 
17  // handle element insertion in a cache
18  virtual void Insert(const Key& key) = 0;
19 
20  // handle request to the key-element in a cache
21  virtual void Touch(const Key& key) = 0;
22 
23  // handle element deletion from a cache
24  virtual void Erase(const Key& key) = 0;
25 
26  // return a key of a replacement candidate
27  virtual const Key& ReplCandidate() const = 0;
28 
29  // clear the cache
30  virtual void Clear() = 0;
31 
32 };
33 
34 template <typename Key>
35 class NoCachePolicy : public ICachePolicy<Key>
36 {
37  public:
38 
39  NoCachePolicy() = default;
40 
41  ~NoCachePolicy() override = default;
42 
43  void Insert(const Key& key) override
44  {
45  key_storage.emplace(key);
46  }
47 
48  void Touch(const Key& key) override
49  {
50  // do not do anything
51  }
52 
53  void Erase(const Key& key) override
54  {
55  key_storage.erase(key);
56  }
57 
58  // return a key of a displacement candidate
59  const Key& ReplCandidate() const override
60  {
61  return *key_storage.crbegin();
62  }
63 
64  // return a key of a displacement candidate
65  void Clear() override
66  {
67  key_storage.clear();
68  }
69 
70  private:
71 
72  std::unordered_set<Key> key_storage;
73 };
74 
75 } // namespace caches
76 
77 #endif // CACHE_POLICY_HPP
virtual void Insert(const Key &key)=0
std::unordered_set< Key > key_storage
Definition: cache_policy.hpp:72
Definition: cache_policy.hpp:11
virtual ~ICachePolicy()
Definition: cache_policy.hpp:15
void Insert(const Key &key) override
Definition: cache_policy.hpp:43
virtual void Touch(const Key &key)=0
void Clear() override
Definition: cache_policy.hpp:65
~NoCachePolicy() override=default
Definition: cache.hpp:11
Definition: cache_policy.hpp:35
virtual const Key & ReplCandidate() const =0
const Key & ReplCandidate() const override
Definition: cache_policy.hpp:59
virtual void Clear()=0
virtual void Erase(const Key &key)=0
void Touch(const Key &key) override
Definition: cache_policy.hpp:48
void Erase(const Key &key) override
Definition: cache_policy.hpp:53