CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
memcache.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_CACHE_MEMCACHE_H
10#define CPPCOMMON_CACHE_MEMCACHE_H
11
12#include "time/timespan.h"
13#include "time/timestamp.h"
14
15#include <mutex>
16#include <map>
17#include <shared_mutex>
18#include <unordered_map>
19
20namespace CppCommon {
21
23
28template <typename TKey, typename TValue>
30{
31public:
32 MemCache() = default;
33 MemCache(const MemCache&) = delete;
34 MemCache(MemCache&&) = delete;
35 ~MemCache() = default;
36
37 MemCache& operator=(const MemCache&) = delete;
39
41 explicit operator bool() const { return !empty(); }
42
44 bool empty() const;
45
47 size_t size() const;
48
50
56 bool emplace(TKey&& key, TValue&& value, const Timespan& timeout = Timespan(0));
57
59
65 bool insert(const TKey& key, const TValue& value, const Timespan& timeout = Timespan(0));
66
68
72 bool find(const TKey& key);
74
79 bool find(const TKey& key, TValue& value);
81
87 bool find(const TKey& key, TValue& value, Timestamp& timeout);
88
90
94 bool remove(const TKey& key);
95
97 void clear();
98
100 void watchdog(const UtcTimestamp& utc = UtcTimestamp());
101
103 void swap(MemCache& cache) noexcept;
104 template <typename UKey, typename UValue>
105 friend void swap(MemCache<UKey, UValue>& cache1, MemCache<UKey, UValue>& cache2) noexcept;
106
107private:
108 mutable std::shared_mutex _lock;
109 Timestamp _timestamp;
110
111 struct MemCacheEntry
112 {
113 TValue value;
114 Timestamp timestamp;
115 Timespan timespan;
116
117 MemCacheEntry() = default;
118 MemCacheEntry(const TValue& v, const Timestamp& ts = Timestamp(), const Timespan& tp = Timespan()) : value(v), timestamp(ts), timespan(tp) {}
119 MemCacheEntry(TValue&& v, const Timestamp& ts = Timestamp(), const Timespan& tp = Timespan()) : value(v), timestamp(ts), timespan(tp) {}
120 };
121
122 std::unordered_map<TKey, MemCacheEntry> _entries_by_key;
123 std::map<Timestamp, TKey> _entries_by_timestamp;
124
125 bool remove_internal(const TKey& key);
126};
127
130} // namespace CppCommon
131
132#include "memcache.inl"
133
134#endif // CPPCOMMON_CACHE_MEMCACHE_H
Memory cache.
Definition memcache.h:30
size_t size() const
Get the memory cache size.
Definition memcache.inl:19
bool remove(const TKey &key)
Remove the cache value with the given key from the memory cache.
Definition memcache.inl:112
bool emplace(TKey &&key, TValue &&value, const Timespan &timeout=Timespan(0))
Emplace a new cache value with the given timeout into the memory cache.
Definition memcache.inl:26
void clear()
Clear the memory cache.
Definition memcache.inl:138
bool empty() const
Is the memory cache empty?
Definition memcache.inl:12
MemCache(const MemCache &)=delete
void watchdog(const UtcTimestamp &utc=UtcTimestamp())
Watchdog the memory cache.
Definition memcache.inl:148
bool find(const TKey &key)
Try to find the cache value by the given key.
Definition memcache.inl:70
MemCache & operator=(const MemCache &)=delete
MemCache & operator=(MemCache &&)=delete
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.
Definition memcache.inl:48
Memory cache inline implementation.
C++ Common project definitions.
Timespan definition.
Timestamp definition.