28#include <shared_mutex>
48 template<
typename Key,
typename Compare = std::less<Key>>
49 concept CacheKey =
requires(
const Key &a,
const Key &b, Compare comp) {
50 { comp(a, b) } -> std::convertible_to<bool>;
66 template<
typename Key, CacheEntry Entry,
typename Compare = std::less<Key>>
71 std::map<Key, Entry, Compare>
74 mutable std::shared_mutex _mutex;
116 std::shared_lock<std::shared_mutex> guard(_mutex);
117 return _entries.find(key) != _entries.end();
126 std::optional<Entry>
get(
const Key &key)
const
128 std::shared_lock<std::shared_mutex> guard(_mutex);
129 auto it = _entries.find(key);
130 if (it == _entries.end()) {
141 void put(
const Key &key,
const Entry &value)
143 std::unique_lock<std::shared_mutex> guard(_mutex);
144 _entries.insert_or_assign(key, value);
153 void put(Key &&key, Entry &&value)
155 std::unique_lock<std::shared_mutex> guard(_mutex);
156 _entries.insert_or_assign(std::move(key), std::move(value));
170 template<
typename... Args>
171 Entry &
emplace(
const Key &key, Args &&...args)
173 std::unique_lock<std::shared_mutex> guard(_mutex);
174 auto [it, inserted] =
175 _entries.try_emplace(key, std::forward<Args>(args)...);
190 template<
typename... Args> Entry &
emplace(Key &&key, Args &&...args)
192 std::unique_lock<std::shared_mutex> guard(_mutex);
193 auto [it, inserted] = _entries.try_emplace(
194 std::move(key), std::forward<Args>(args)...);
205 std::unique_lock<std::shared_mutex> guard(_mutex);
206 return _entries.erase(key) > 0;
215 const std::function<
bool(
const Key &,
const Entry &)> &predicate)
217 std::unique_lock<std::shared_mutex> guard(_mutex);
218 for (
auto it = _entries.begin(); it != _entries.end();) {
219 if (predicate(it->first, it->second)) {
220 it = _entries.erase(it);
232 void apply(
const std::function<
void(
const Key &, Entry &)> &function)
234 std::unique_lock<std::shared_mutex> guard(_mutex);
235 for (
auto &[key, entry]: _entries) {
236 function(key, entry);
245 std::unique_lock<std::shared_mutex> guard(_mutex);
253 std::map<Key, Entry, Compare>::size_type
size(
void)
const
255 std::shared_lock<std::shared_mutex> guard(_mutex);
256 return _entries.size();
265 std::shared_lock<std::shared_mutex> guard(_mutex);
266 return _entries.empty();
A thread-safe cache for storing key-value pairs.
bool contains(const Key &key) const
Check if the cache contains a specific key.
~Cache(void)=default
Default destructor for the Cache class.
Cache(const Cache &other)=delete
Deleted copy constructor to prevent copying of the cache.
std::optional< Entry > get(const Key &key) const
Retrieve the value associated with a specific key.
Entry & emplace(const Key &key, Args &&...args)
Emplace a new entry in the cache with the given key and constructor arguments.
bool erase(const Key &key)
Remove an entry from the cache.
void put(Key &&key, Entry &&value)
Add or update a key-value pair in the cache using move semantics.
Cache(void)=default
Default constructor for the Cache class.
Cache & operator=(Cache &&other) noexcept=delete
Deleted move assignment operator to prevent moving of the cache.
bool empty(void) const
Check if the cache is empty.
void put(const Key &key, const Entry &value)
Add or update a key-value pair in the cache.
void erase_if(const std::function< bool(const Key &, const Entry &)> &predicate)
Remove entries from the cache based on a predicate.
Cache & operator=(const Cache &other)=delete
Deleted copy assignment operator to prevent copying of the cache.
std::map< Key, Entry, Compare >::size_type size(void) const
Get the number of entries in the cache.
void apply(const std::function< void(const Key &, Entry &)> &function)
Apply a function to each entry in the cache.
Entry & emplace(Key &&key, Args &&...args)
Emplace a new entry in the cache with the given key and constructor arguments using move semantics.
void clear(void)
Clear all entries from the cache.
Cache(Cache &&other) noexcept=delete
Deleted move constructor to prevent moving of the cache.
Concept to constrain cache entry type.
Concept to constrain cache key type.
The utility namespace contains classes and functions for the utility project.