9 #ifndef CPPCOMMON_CACHE_MEMCACHE_H
10 #define CPPCOMMON_CACHE_MEMCACHE_H
17 #include <shared_mutex>
18 #include <unordered_map>
28 template <
typename TKey,
typename TValue>
41 explicit operator bool()
const {
return !
empty(); }
72 bool find(
const TKey& key);
79 bool find(
const TKey& key, TValue& value);
87 bool find(
const TKey& key, TValue& value,
Timestamp& timeout);
94 bool remove(
const TKey& key);
104 template <
typename UKey,
typename UValue>
108 mutable std::shared_mutex _lock;
117 MemCacheEntry() =
default;
122 std::unordered_map<TKey, MemCacheEntry> _entries_by_key;
123 std::map<Timestamp, TKey> _entries_by_timestamp;
125 bool remove_internal(
const TKey& key);
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.
MemCache & operator=(MemCache &&)=delete
MemCache & operator=(const MemCache &)=delete
void clear()
Clear the memory cache.
void swap(MemCache &cache) noexcept
Swap two instances.
bool empty() const
Is the memory cache empty?
MemCache(const MemCache &)=delete
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.
MemCache(MemCache &&)=delete
friend void swap(MemCache< UKey, UValue > &cache1, MemCache< UKey, UValue > &cache2) noexcept
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.
Memory cache inline implementation.
C++ Common project definitions.