Cachemere
Modular Caching Library for C++
Using Custom Keys

This page is meant to guide you through the usage of a custom key with a cache.

Key Requirements

  • Must have both a move constructor and a move assignment operator.
  • Must implement equality operator (bool operator==(const Self&) const;).
  • Must be hashable by the hasher passed as template parameter to the cache (by default, absl::Hash<Key>).

The key does not need to be copyable.

Usage Example

With <tt>absl::Hash</tt> (default hasher)

#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
template<typename H> friend H AbslHashValue(H h, const ComplexKey& s)
{
return H::combine(std::move(h), s.first, s.second);
}
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
usig ValueT = int;
using Cache = cachemere::presets::memory::LRUCache<ComplexKey,
ValueT,

With a custom hasher

#include <string>
#include <cachemere.h>
struct ComplexKey {
std::string first;
std::string second;
bool operator==(const ComplexKey& other) const {
return
}
size_t capacity() const {
return first.capacity() + second.capacity();
}
};
struct VeryBadHash {
size_t operator(const ComplexKey&) {
return 0;
}
};
usig ValueT = int;
using Cache = cachemere::presets::memory::LRUCache<ComplexKey,
ValueT,
VeryBadHash>;
cachemere::measurement::CapacityDynamicallyAllocated
Get the size of an object via a user-defined capacity() method.
Definition: measurement.h:20
cachemere::measurement::SizeOf
Get the size of an object via sizeof().
Definition: measurement.h:15
cachemere::Cache
Thread-safe memory-restricted cache.
Definition: cache.h:63