Cachemere
Modular Caching Library for C++
|
1 #ifndef CACHEMERE_CACHE_H
2 #define CACHEMERE_CACHE_H
11 #include <absl/container/node_hash_map.h>
12 #include <absl/hash/hash.h>
15 # pragma warning(push)
16 # pragma warning(disable : 4244)
17 # pragma warning(disable : 4018)
20 #include <boost/accumulators/accumulators.hpp>
21 #include <boost/accumulators/statistics/rolling_mean.hpp>
22 #include <boost/accumulators/statistics/stats.hpp>
23 #include <boost/hana.hpp>
30 #include "measurement.h"
31 #include "detail/transparent_eq.h"
32 #include "detail/traits.h"
51 template<
typename Key,
53 template<
class,
class,
class>
54 class InsertionPolicy,
55 template<
class,
class,
class>
57 template<
class,
class,
class>
58 class ConstraintPolicy,
61 typename KeyHash = absl::Hash<Key>,
62 bool ThreadSafe =
true>
66 using MyInsertionPolicy = InsertionPolicy<Key, KeyHash, Value>;
67 using MyEvictionPolicy = EvictionPolicy<Key, KeyHash, Value>;
68 using MyConstraintPolicy = ConstraintPolicy<Key, KeyHash, Value>;
70 using LockGuard = std::unique_lock<std::recursive_mutex>;
74 template<
typename... Args>
Cache(Args... args);
82 template<
typename C,
typename... Args>
Cache(C& collection, std::tuple<Args...> args);
88 template<
typename KeyView>
bool contains(
const KeyView& key)
const;
94 template<
typename KeyView> std::optional<Value>
find(
const KeyView& key)
const;
103 template<
typename C>
void collect_into(C& container)
const;
111 bool insert(Key key, Value value);
117 bool remove(
const Key& key);
127 template<
typename P>
void retain(P predicate_fn);
132 template<
typename F>
void for_each(F unary_function);
173 [[nodiscard]]
double hit_rate()
const;
193 LockGuard lock()
const;
194 LockGuard lock(std::defer_lock_t defer_lock_tag)
const;
195 std::pair<LockGuard, LockGuard> lock_pair(
CacheType& other)
const;
197 template<
typename C>
void import(C& collection);
202 using DataMap = absl::node_hash_map<Key, CacheItem, KeyHash, detail::TransparentEq<Key>>;
204 using DataMapIt =
typename DataMap::iterator;
206 using MyInsertionPolicySP = std::unique_ptr<MyInsertionPolicy>;
207 using MyEvictionPolicySP = std::unique_ptr<MyEvictionPolicy>;
208 using MyConstraintPolicySP = std::unique_ptr<MyConstraintPolicy>;
210 using RollingMeanTag = boost::accumulators::tag::rolling_mean;
211 using RollingMeanStatistics = boost::accumulators::stats<RollingMeanTag>;
212 using MeanAccumulator = boost::accumulators::accumulator_set<uint32_t, RollingMeanStatistics>;
214 uint32_t m_statistics_window_size = 1000;
216 MyInsertionPolicySP m_insertion_policy;
217 MyEvictionPolicySP m_eviction_policy;
218 MyConstraintPolicySP m_constraint_policy;
220 MeasureKey m_measure_key;
221 MeasureValue m_measure_value;
223 mutable std::recursive_mutex m_mutex;
226 mutable MeanAccumulator m_hit_rate_acc;
227 mutable MeanAccumulator m_byte_hit_rate_acc;
229 bool check_insert(
const Key& candidate_key,
const CacheItem& item);
230 bool check_replace(
const Key& candidate_key,
const CacheItem& old_item,
const CacheItem& new_item);
232 void insert_or_update(Key&& key,
CacheItem&& value);
233 void remove(DataMapIt it);
235 void on_insert(
const Key& key,
const CacheItem& item)
const;
236 void on_update(
const Key& key,
const CacheItem& old_item,
const CacheItem& new_item)
const;
237 void on_cache_hit(
const Key& key,
const CacheItem& item)
const;
238 template<
typename KeyView>
void on_cache_miss(
const KeyView& key)
const;
239 void on_evict(
const Key& key,
const CacheItem& item)
const;
244 template<
class,
class,
class>
246 template<
class,
class,
class>
248 template<
class,
class,
class>
254 void swap(
Cache<K, V, I, E, C, SV, SK, KH, TS>& lhs,
Cache<K, V, I, E, C, SV, SK, KH, TS>& rhs) noexcept;
MyInsertionPolicy & insertion_policy()
Get a reference to the insertion policy used by the cache.
Definition: cache.hpp:389
MyEvictionPolicy & eviction_policy()
Get a reference to the eviction policy used by the cache.
Definition: cache.hpp:423
bool remove(const Key &key)
Remove a key and its value from the cache.
void swap(CacheType &other) noexcept
Swaps the current cache with another cache of the same type.
Definition: cache.hpp:288
void for_each(F unary_function)
Apply a function to all objects in cache.
Definition: cache.hpp:268
void retain(P predicate_fn)
Retain all objects matching a predicate.
Definition: cache.hpp:240
void clear()
Clears the cache contents.
Definition: cache.hpp:213
double byte_hit_rate() const
Compute and return the running byte hit rate of the cache, in bytes.
Definition: cache.hpp:508
A wrapper for items stored in the cache.
Definition: item.h:10
size_t number_of_items() const
Get the number of items currently stored in the cache.
Definition: cache.hpp:336
bool contains(const KeyView &key) const
Check whether a given key is stored in the cache.
Definition: cache.hpp:66
bool insert(Key key, Value value)
Insert a key/value pair in the cache.
Definition: cache.hpp:148
std::optional< Value > find(const KeyView &key) const
Find a given key in cache returning the associated value when it exists.
double hit_rate() const
Compute and return the running hit rate of the cache.
Definition: cache.hpp:491
void update_constraint(Args... args)
Update the cache constraint.
Definition: cache.hpp:355
Thread-safe memory-restricted cache.
Definition: cache.h:63
Root namespace.
Definition: cache.h:35
MyConstraintPolicy & constraint_policy()
Get a reference to the constraint policy used by the cache.
Definition: cache.hpp:457
Cache(Args... args)
Simple constructor.
Definition: cache.hpp:16
void collect_into(C &container) const
Copy the cache contents in the provided container.
uint32_t statistics_window_size() const
Get the size of the sliding window used for computing statistics.
Definition: cache.hpp:525