AWS SDK for C++

AWS SDK for C++ Version 1.11.440

Loading...
Searching...
No Matches
Cache.h
1
6#pragma once
7
8#include <aws/core/utils/DateTime.h>
9#include <aws/core/utils/memory/stl/AWSMap.h>
10#include <chrono>
11
12namespace Aws
13{
14 namespace Utils
15 {
19 template <typename TKey, typename TValue>
20 class Cache
21 {
22 public:
27 explicit Cache(size_t initialSize = 1000) : m_maxSize(initialSize)
28 {
29 }
30
31 struct Value
32 {
34 TValue val;
35 };
36
42 bool Get(const TKey& key, TValue& value) const
43 {
44 auto it = m_entries.find(key);
45 if (it == m_entries.end())
46 {
47 return false;
48 }
49
50 if (DateTime::Now() > it->second.expiration)
51 {
52 return false;
53 }
54
55 value = it->second.val;
56 return true;
57 }
58
71 template<typename UValue>
72 void Put(TKey&& key, UValue&& val, std::chrono::milliseconds duration)
73 {
74 auto it = m_entries.find(key);
75 const DateTime expiration = DateTime::Now() + duration;
76 if (it != m_entries.end())
77 {
78 it->second.val = std::forward<UValue>(val);
79 it->second.expiration = expiration;
80 return;
81 }
82
83 if (m_entries.size() >= m_maxSize)
84 {
85 Prune(); // removes expired/expiring elements
86 }
87
88 m_entries.emplace(std::move(key), Value { expiration, std::forward<UValue>(val) });
89 }
90
91 template<typename UValue>
92 void Put(const TKey& key, UValue&& val, std::chrono::milliseconds duration)
93 {
94 auto it = m_entries.find(key);
95 const DateTime expiration = DateTime::Now() + duration;
96 if (it != m_entries.end())
97 {
98 it->second.val = std::forward<UValue>(val);
99 it->second.expiration = expiration;
100 return;
101 }
102
103 if (m_entries.size() >= m_maxSize)
104 {
105 Prune(); // removes expired/expiring elements
106 }
107
108 m_entries.emplace(key, Value { expiration, std::forward<UValue>(val) });
109 }
110
117 using TransformFunction = std::function<Value(const TKey &, Value &)>;
119 for (auto it = m_entries.begin(); it != m_entries.end(); ++it) {
120 it->second = function(it->first, it->second);
121 }
122 }
123
130 using FilterFunction = std::function<bool(const TKey &, const Value&)>;
131 void Filter(FilterFunction function) {
132 auto it = m_entries.begin();
133 while (it != m_entries.end()) {
134 auto shouldFilter = function(it->first, it->second);
135 if (shouldFilter) {
136 it = m_entries.erase(it);
137 } else {
138 ++it;
139 }
140 }
141 }
142
143 private:
144
145 void Prune()
146 {
147 auto mostExpiring = m_entries.begin();
148 // remove the expired ones. If none expired, remove the one that's closest to expiring.
149 for (auto it = m_entries.begin(); it != m_entries.end();)
150 {
151 if (DateTime::Now() > it->second.expiration)
152 {
153 it = m_entries.erase(it);
154 }
155 else
156 {
157 if (it->second.expiration < mostExpiring->second.expiration)
158 {
159 mostExpiring = it;
160 }
161 ++it;
162 }
163 }
164
165 // if nothing was erased. Remove the most expiring element.
166 if (m_entries.size() >= m_maxSize)
167 {
168 m_entries.erase(mostExpiring);
169 }
170 }
171
172 Aws::Map<TKey, Value> m_entries;
173 const size_t m_maxSize;
174 };
175 }
176}
std::function< bool(const TKey &, const Value &)> FilterFunction
Definition Cache.h:130
bool Get(const TKey &key, TValue &value) const
Definition Cache.h:42
std::function< Value(const TKey &, Value &)> TransformFunction
Definition Cache.h:117
Cache(size_t initialSize=1000)
Definition Cache.h:27
void Transform(TransformFunction function)
Definition Cache.h:118
void Filter(FilterFunction function)
Definition Cache.h:131
void Put(TKey &&key, UValue &&val, std::chrono::milliseconds duration)
Definition Cache.h:72
void Put(const TKey &key, UValue &&val, std::chrono::milliseconds duration)
Definition Cache.h:92
static DateTime Now()
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition AWSMap.h:20