19 template <
typename TKey,
typename TValue>
27 explicit Cache(
size_t initialSize = 1000) : m_maxSize(initialSize)
36 bool Get(
const TKey& key, TValue& value)
const
38 auto it = m_entries.find(key);
39 if (it == m_entries.end())
49 value = it->second.val;
65 template<
typename UValue>
66 void Put(TKey&& key, UValue&& val, std::chrono::milliseconds duration)
68 auto it = m_entries.find(key);
70 if (it != m_entries.end())
72 it->second.val = std::forward<UValue>(val);
73 it->second.expiration = expiration;
77 if (m_entries.size() >= m_maxSize)
82 m_entries.emplace(std::move(key), Value { expiration, std::forward<UValue>(val) });
85 template<
typename UValue>
86 void Put(
const TKey& key, UValue&& val, std::chrono::milliseconds duration)
88 auto it = m_entries.find(key);
90 if (it != m_entries.end())
92 it->second.val = std::forward<UValue>(val);
93 it->second.expiration = expiration;
97 if (m_entries.size() >= m_maxSize)
102 m_entries.emplace(key, Value { expiration, std::forward<UValue>(val) });
109 auto mostExpiring = m_entries.begin();
111 for (
auto it = m_entries.begin(); it != m_entries.end();)
115 it = m_entries.erase(it);
119 if (it->second.expiration < mostExpiring->second.expiration)
128 if (m_entries.size() >= m_maxSize)
130 m_entries.erase(mostExpiring);
141 const size_t m_maxSize;
bool Get(const TKey &key, TValue &value) const
Cache(size_t initialSize=1000)
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