11 template <
typename TKey,
typename TValue>
14 std::shared_lock<std::shared_mutex> locker(_lock);
15 return _entries_by_key.empty();
18 template <
typename TKey,
typename TValue>
21 std::shared_lock<std::shared_mutex> locker(_lock);
22 return _entries_by_key.size();
25 template <
typename TKey,
typename TValue>
28 std::unique_lock<std::shared_mutex> locker(_lock);
34 if (timeout.
total() > 0)
37 _timestamp = (current <= _timestamp) ? _timestamp + 1 : current;
38 _entries_by_key.insert(std::make_pair(key, MemCacheEntry(std::move(value), _timestamp, timeout)));
39 _entries_by_timestamp.insert(std::make_pair(_timestamp, key));
42 _entries_by_key.emplace(std::make_pair(std::move(key), MemCacheEntry(std::move(value))));
47 template <
typename TKey,
typename TValue>
50 std::unique_lock<std::shared_mutex> locker(_lock);
56 if (timeout.
total() > 0)
59 _timestamp = (current <= _timestamp) ? _timestamp + 1 : current;
60 _entries_by_key.insert(std::make_pair(key, MemCacheEntry(value, _timestamp, timeout)));
61 _entries_by_timestamp.insert(std::make_pair(_timestamp, key));
64 _entries_by_key.insert(std::make_pair(key, MemCacheEntry(value)));
69 template <
typename TKey,
typename TValue>
72 std::shared_lock<std::shared_mutex> locker(_lock);
75 auto it = _entries_by_key.find(key);
76 if (it == _entries_by_key.end())
82 template <
typename TKey,
typename TValue>
85 std::shared_lock<std::shared_mutex> locker(_lock);
88 auto it = _entries_by_key.find(key);
89 if (it == _entries_by_key.end())
92 value = it->second.value;
96 template <
typename TKey,
typename TValue>
99 std::shared_lock<std::shared_mutex> locker(_lock);
102 auto it = _entries_by_key.find(key);
103 if (it == _entries_by_key.end())
106 value = it->second.value;
107 timeout = it->second.timestamp + it->second.timespan;
111 template <
typename TKey,
typename TValue>
114 std::unique_lock<std::shared_mutex> locker(_lock);
116 return remove_internal(key);
119 template <
typename TKey,
typename TValue>
123 auto it = _entries_by_key.
find(key);
124 if (it == _entries_by_key.end())
128 if (it->second.timestamp.total() > 0)
129 _entries_by_timestamp.erase(it->second.timestamp);
132 _entries_by_key.erase(it);
137 template <
typename TKey,
typename TValue>
140 std::unique_lock<std::shared_mutex> locker(_lock);
143 _entries_by_key.clear();
144 _entries_by_timestamp.clear();
147 template <
typename TKey,
typename TValue>
150 std::unique_lock<std::shared_mutex> locker(_lock);
153 auto it_entry_by_timestamp = _entries_by_timestamp.begin();
154 while (it_entry_by_timestamp != _entries_by_timestamp.end())
157 auto it_entry_by_key = _entries_by_key.find(it_entry_by_timestamp->second);
158 if ((it_entry_by_key->second.timestamp + it_entry_by_key->second.timespan) <= utc)
161 _entries_by_key.erase(it_entry_by_key);
162 _entries_by_timestamp.erase(it_entry_by_timestamp);
163 it_entry_by_timestamp = _entries_by_timestamp.begin();
171 template <
typename TKey,
typename TValue>
174 std::unique_lock<std::shared_mutex> locker1(_lock);
175 std::unique_lock<std::shared_mutex> locker2(cache._lock);
178 swap(_timestamp, cache._timestamp);
179 swap(_entries_by_key, cache._entries_by_key);
180 swap(_entries_by_timestamp, cache._entries_by_timestamp);
183 template <
typename TKey,
typename TValue>
size_t size() const
Get the memory cache size.
bool remove(const TKey &key)
Remove the cache value with the given key from the memory cache.
bool emplace(TKey &&key, TValue &&value, const Timespan &timeout=Timespan(0))
Emplace a new cache value with the given timeout into the memory cache.
void clear()
Clear the memory cache.
void swap(MemCache &cache) noexcept
Swap two instances.
bool empty() const
Is the memory cache empty?
void watchdog(const UtcTimestamp &utc=UtcTimestamp())
Watchdog the memory cache.
bool find(const TKey &key)
Try to find the cache value by the given key.
bool insert(const TKey &key, const TValue &value, const Timespan &timeout=Timespan(0))
Insert a new cache value with the given timeout into the memory cache.
int64_t total() const noexcept
Get total value of the current timespan (total nanoseconds)
C++ Common project definitions.
void swap(FileCache &cache1, FileCache &cache2) noexcept
void swap(MemCache< TKey, TValue > &cache1, MemCache< TKey, TValue > &cache2) noexcept