8#include <aws/core/utils/DateTime.h>
9#include <aws/core/utils/memory/stl/AWSMap.h>
19 template <
typename TKey,
typename TValue>
27 explicit Cache(
size_t initialSize = 1000) : m_maxSize(initialSize)
42 bool Get(
const TKey& key, TValue& value)
const
44 auto it = m_entries.find(key);
45 if (it == m_entries.end())
55 value = it->second.val;
71 template<
typename UValue>
72 void Put(TKey&& key, UValue&& val, std::chrono::milliseconds duration)
74 auto it = m_entries.find(key);
76 if (it != m_entries.end())
78 it->second.val = std::forward<UValue>(val);
79 it->second.expiration = expiration;
83 if (m_entries.size() >= m_maxSize)
88 m_entries.emplace(std::move(key),
Value { expiration, std::forward<UValue>(val) });
91 template<
typename UValue>
92 void Put(
const TKey& key, UValue&& val, std::chrono::milliseconds duration)
94 auto it = m_entries.find(key);
96 if (it != m_entries.end())
98 it->second.val = std::forward<UValue>(val);
99 it->second.expiration = expiration;
103 if (m_entries.size() >= m_maxSize)
108 m_entries.emplace(key,
Value { expiration, std::forward<UValue>(val) });
119 for (
auto it = m_entries.begin(); it != m_entries.end(); ++it) {
120 it->second = function(it->first, it->second);
132 auto it = m_entries.begin();
133 while (it != m_entries.end()) {
134 auto shouldFilter = function(it->first, it->second);
136 it = m_entries.erase(it);
147 auto mostExpiring = m_entries.begin();
149 for (
auto it = m_entries.begin(); it != m_entries.end();)
153 it = m_entries.erase(it);
157 if (it->second.expiration < mostExpiring->second.expiration)
166 if (m_entries.size() >= m_maxSize)
168 m_entries.erase(mostExpiring);
173 const size_t m_maxSize;
std::function< bool(const TKey &, const Value &)> FilterFunction
bool Get(const TKey &key, TValue &value) const
std::function< Value(const TKey &, Value &)> TransformFunction
Cache(size_t initialSize=1000)
void Transform(TransformFunction function)
void Filter(FilterFunction function)
void Put(TKey &&key, UValue &&val, std::chrono::milliseconds duration)
void Put(const TKey &key, UValue &&val, std::chrono::milliseconds duration)
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map