35 #include <unordered_map> 44 template <
typename K,
typename V,
typename Duration = std::chrono::seconds>
47 TTLCache(
const Duration &ttl) : _ttl(ttl) {};
50 std::lock_guard<std::mutex> lock(_lock);
51 auto it = _cache.find(key);
52 if (it == _cache.end()) {
54 }
else if (getNowDurationSinceEpoch() - (*it).second.second > _ttl) {
62 void put(
const K &key,
const V &value) {
63 std::lock_guard<std::mutex> lock(_lock);
67 getNowDurationSinceEpoch()
72 void erase(
const K &key) {
73 std::lock_guard<std::mutex> lock(_lock);
78 Duration getNowDurationSinceEpoch() {
79 return std::chrono::duration_cast<Duration>(std::chrono::steady_clock::now().time_since_epoch());
82 std::unordered_map<K, std::pair<V, Duration>> _cache;
Definition: Option.hpp:49
Definition: Account.cpp:8
Definition: TTLCache.h:45