AWS SDK for C++  1.9.125
AWS SDK for C++
Cache.h
Go to the documentation of this file.
1 
6 #pragma once
7 
10 #include <chrono>
11 
12 namespace 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 
36  bool Get(const TKey& key, TValue& value) const
37  {
38  auto it = m_entries.find(key);
39  if (it == m_entries.end())
40  {
41  return false;
42  }
43 
44  if (DateTime::Now() > it->second.expiration)
45  {
46  return false;
47  }
48 
49  value = it->second.val;
50  return true;
51  }
52 
65  template<typename UValue>
66  void Put(TKey&& key, UValue&& val, std::chrono::milliseconds duration)
67  {
68  auto it = m_entries.find(key);
69  const DateTime expiration = DateTime::Now() + duration;
70  if (it != m_entries.end())
71  {
72  it->second.val = std::forward<UValue>(val);
73  it->second.expiration = expiration;
74  return;
75  }
76 
77  if (m_entries.size() >= m_maxSize)
78  {
79  Prune(); // removes expired/expiring elements
80  }
81 
82  m_entries.emplace(std::move(key), Value { expiration, std::forward<UValue>(val) });
83  }
84 
85  template<typename UValue>
86  void Put(const TKey& key, UValue&& val, std::chrono::milliseconds duration)
87  {
88  auto it = m_entries.find(key);
89  const DateTime expiration = DateTime::Now() + duration;
90  if (it != m_entries.end())
91  {
92  it->second.val = std::forward<UValue>(val);
93  it->second.expiration = expiration;
94  return;
95  }
96 
97  if (m_entries.size() >= m_maxSize)
98  {
99  Prune(); // removes expired/expiring elements
100  }
101 
102  m_entries.emplace(key, Value { expiration, std::forward<UValue>(val) });
103  }
104 
105  private:
106 
107  void Prune()
108  {
109  auto mostExpiring = m_entries.begin();
110  // remove the expired ones. If none expired, remove the one that's closest to expiring.
111  for (auto it = m_entries.begin(); it != m_entries.end();)
112  {
113  if (DateTime::Now() > it->second.expiration)
114  {
115  it = m_entries.erase(it);
116  }
117  else
118  {
119  if (it->second.expiration < mostExpiring->second.expiration)
120  {
121  mostExpiring = it;
122  }
123  ++it;
124  }
125  }
126 
127  // if nothing was erased. Remove the most expiring element.
128  if (m_entries.size() >= m_maxSize)
129  {
130  m_entries.erase(mostExpiring);
131  }
132  }
133 
134  struct Value
135  {
136  DateTime expiration;
137  TValue val;
138  };
139 
140  Aws::Map<TKey, Value> m_entries;
141  const size_t m_maxSize;
142  };
143  }
144 }
Aws::Utils::DateTime
Definition: DateTime.h:55
Aws::Utils::DateTime::Now
static DateTime Now()
Aws::Utils::Cache::Get
bool Get(const TKey &key, TValue &value) const
Definition: Cache.h:36
Aws::Utils::Cache
Definition: Cache.h:21
DateTime.h
Aws::Utils::Cache::Put
void Put(TKey &&key, UValue &&val, std::chrono::milliseconds duration)
Definition: Cache.h:66
Aws::Utils::Cache::Put
void Put(const TKey &key, UValue &&val, std::chrono::milliseconds duration)
Definition: Cache.h:86
Aws::Utils::Cache::Cache
Cache(size_t initialSize=1000)
Definition: Cache.h:27
Aws
Definition: AccessManagementClient.h:15
AWSMap.h
Aws::Map
std::map< K, V, std::less< K >, Aws::Allocator< std::pair< const K, V > > > Map
Definition: AWSMap.h:19