15 #include <unordered_map> 22 template <
typename KEY_TYPE,
typename VALUE_TYPE>
43 typedef std::unordered_map<KEY_TYPE, CacheEntry>
MapType;
64 return static_cast<uint32_t
>(
mCache.size());
79 olderThan = now - olderThan;
84 if ((*iter).second.lastUsedTime < olderThan)
104 if ((*iter).second.expiryTime <= now)
106 iter =
mCache.erase(iter);
129 auto iter =
mCache.find(key);
146 auto iter =
mCache.find(key);
152 (*iter).second.lastUsedTime = std::numeric_limits<uint64_t>::max();
164 auto iter =
mCache.find(key);
170 (*iter).second.expiryTime = std::numeric_limits<uint64_t>::max();
181 auto iter =
mCache.find(key);
187 (*iter).second.expiryTime = time;
208 kvp.second.expiryTime = 0;
221 typename MapType::iterator iter =
mCache.find(key);
224 entry = (*iter).second;
229 entry.lastUsedTime = now;
235 if (now <= std::numeric_limits<uint64_t>::max() -
mExpiryAge)
241 entry.expiryTime = std::numeric_limits<uint64_t>::max();
252 auto iter =
mCache.find(key);
267 auto iter =
mCache.find(key);
270 result = (*iter).second.data;
273 return iter !=
mCache.end();
283 result.push_back(kvp.first);
302 for (
auto& kvp : copy)
311 for (
const auto& kvp :
mCache)
313 if (kvp.second.expiryTime <= now)
331 for (
auto& kvp : copy)
333 if (kvp.second.expiryTime <= now)
bool GetEntry(const KEY_TYPE &key, VALUE_TYPE &result)
Definition: cache.h:265
bool MarkEntryNeverUnused(const KEY_TYPE &key)
Definition: cache.h:144
CacheEntry()
Definition: cache.h:28
bool ContainsEntry(const KEY_TYPE &key) const
Definition: cache.h:290
void SetExpiryAge(uint64_t age)
Definition: cache.h:54
uint64_t mExpiryAge
The amount of time after which entries expire.
Definition: cache.h:342
uint64_t GetSystemTimeMilliseconds()
VALUE_TYPE data
The cached data.
Definition: cache.h:37
void SetEntry(const KEY_TYPE &key, VALUE_TYPE data)
Definition: cache.h:215
void ForEachExpired(VisitorFunc func)
Definition: cache.h:325
KEY_TYPE key
Definition: cache.h:34
uint32_t Timestamp
Definition: coretypes.h:27
MapType mCache
The mapping of key to the data.
Definition: cache.h:341
std::function< void(CacheEntry &entry)> VisitorFunc
Definition: cache.h:40
Cache()
Definition: cache.h:46
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
bool SetEntryExpiryTime(const KEY_TYPE &key, uint64_t time)
Definition: cache.h:179
bool HasExpiredEntries() const
Definition: cache.h:308
void RemoveEntry(const KEY_TYPE &key)
Definition: cache.h:250
void Clear()
Definition: cache.h:118
uint64_t expiryTime
The time the data will expire.
Definition: cache.h:35
void ForEach(VisitorFunc func)
Definition: cache.h:298
std::unordered_map< KEY_TYPE, CacheEntry > MapType
Definition: cache.h:43
bool MarkEntryNeverExpires(const KEY_TYPE &key)
Definition: cache.h:162
void PurgeExpired()
Definition: cache.h:98
void PurgeUnused(uint64_t olderThan)
Definition: cache.h:70
bool MarkEntryUsed(const KEY_TYPE &key)
Definition: cache.h:127
void GetKeys(std::vector< KEY_TYPE > &result)
Definition: cache.h:279
uint64_t lastUsedTime
The last time the data was used.
Definition: cache.h:36
uint32_t GetSize() const
Definition: cache.h:62
void ExpireAll()
Definition: cache.h:204
bool ExpireEntry(const KEY_TYPE &key)
Definition: cache.h:196