Union::LruCache Class
template <typename Key, typename Value, std::size_t MaxSize = 100> class Union::LruCacheA simple least-recently-used cache template. More...
Header: | #include <LruCache> |
CMake: | find_package(Union REQUIRED) target_link_libraries(mytarget PRIVATE Union::Union) |
Public Functions
void | clear() |
bool | contains(const Key &key) |
void | insert(const Key &key, const Value &value) |
std::optional<Value> | value(const Key &key) |
Detailed Description
This will cache up to \p
MaxSize values. If the maximum size is reached, inserting a new item will evict the least recently used item from the cache.
Values are expected to be copyable types. Do not use this to store raw pointers, instead use some form of managed pointer when storing heap-allocated data.
Member Function Documentation
void LruCache::clear()
Clear the cache of all entries.
bool LruCache::contains(const Key &key)
Returns if the cache contains the specified key.
void LruCache::insert(const Key &key, const Value &value)
Insert a new value associated with the given key into the cache.
If the cache already contains a value matching the given key, it will replace the existing value.
std::optional<Value> LruCache::value(const Key &key)
Returns the value associated with a given key, if the key is in the cache.
If not, it will return std::nullopt
. Using this function will update the given value to be the most recently used value.