15 template <
typename K,
typename T>
19 const std::chrono::seconds cLifetime;
20 std::map<K, std::pair<const std::chrono::system_clock::time_point, const T>> mCachedItems;
25 explicit MemoryCache(std::chrono::seconds lifetime) : cLifetime{lifetime}
32 void Add(K key,
const T &item)
35 key, std::make_pair(std::chrono::system_clock::now(), item));
45 auto _cachedItemItr{mCachedItems.find(key)};
47 if (_cachedItemItr != mCachedItems.end())
49 const auto cNow{std::chrono::system_clock::now()};
50 if (cNow - _cachedItemItr->second.first < cLifetime)
52 item = _cachedItemItr->second.second;
58 mCachedItems.erase(_cachedItemItr);
Memory cache helper class with expiration time.
Definition: memory_cache.h:17
bool TryGet(K key, T &item)
Try to get an item from the cache.
Definition: memory_cache.h:43
MemoryCache(std::chrono::seconds lifetime)
Constructor.
Definition: memory_cache.h:25
void Add(K key, const T &item)
Add an item to the cache.
Definition: memory_cache.h:32
AUTOSAR application namespace.
Definition: diag_message_handler.cpp:5