|
Grok 20.3.2
|
Thread-safe LRU cache with configurable capacity and eviction callback. More...
#include <LRUCache.h>
Public Types | |
| using | SizeFunc = std::function<size_t(const Value&)> |
| using | EvictFunc = std::function<void(const Key&, Value&&)> |
Public Member Functions | |
| LRUCache (size_t maxBytes, SizeFunc sizeFunc, EvictFunc evictFunc=nullptr) | |
| Construct an LRU cache. | |
| ~LRUCache ()=default | |
| LRUCache (const LRUCache &)=delete | |
| LRUCache & | operator= (const LRUCache &)=delete |
| LRUCache (LRUCache &&)=default | |
| LRUCache & | operator= (LRUCache &&)=default |
| void | put (const Key &key, Value value) |
| Insert or update an entry. | |
| Value * | get (const Key &key) |
| Look up an entry. | |
| bool | contains (const Key &key) const |
| Check if a key exists without promoting it in the LRU order. | |
| bool | erase (const Key &key) |
| Remove a specific entry. | |
| void | clear () |
| Clear all entries. | |
| size_t | size () const |
| size_t | currentBytes () const |
| size_t | maxBytes () const |
| void | setMaxBytes (size_t maxBytes) |
Private Types | |
| using | ListType = std::list<std::pair<Key, Value>> |
Private Member Functions | |
| void | evictWhileOverCapacity () |
Private Attributes | |
| size_t | maxBytes_ |
| size_t | currentBytes_ |
| SizeFunc | sizeFunc_ |
| EvictFunc | evictFunc_ |
| ListType | list_ |
| std::unordered_map< Key, typename ListType::iterator > | map_ |
| std::mutex | mutex_ |
Thread-safe LRU cache with configurable capacity and eviction callback.
Key must be hashable. Value must be moveable. The sizeFunc callback returns the cost (in bytes) of a value, enabling memory-based eviction rather than count-based.
| Key | key type (must be hashable) |
| Value | value type (must be moveable) |
| using grk::LRUCache< Key, Value >::EvictFunc = std::function<void(const Key&, Value&&)> |
|
private |
| using grk::LRUCache< Key, Value >::SizeFunc = std::function<size_t(const Value&)> |
|
inline |
Construct an LRU cache.
| maxBytes | high water mark in bytes (0 = unlimited) |
| sizeFunc | returns size in bytes for a given value |
| evictFunc | called when an entry is evicted (optional) |
References currentBytes_, evictFunc_, maxBytes(), maxBytes_, and sizeFunc_.
Referenced by LRUCache(), LRUCache(), operator=(), and operator=().
|
default |
|
delete |
References LRUCache().
|
default |
References LRUCache().
|
inline |
Clear all entries.
References currentBytes_, list_, map_, and mutex_.
|
inline |
|
inline |
References currentBytes_, and mutex_.
|
inline |
Remove a specific entry.
References currentBytes_, list_, map_, mutex_, and sizeFunc_.
|
inlineprivate |
References currentBytes_, evictFunc_, list_, map_, maxBytes_, and sizeFunc_.
Referenced by put(), and setMaxBytes().
|
inline |
|
inline |
References maxBytes_.
Referenced by LRUCache(), and setMaxBytes().
|
delete |
References LRUCache().
|
default |
References LRUCache().
|
inline |
Insert or update an entry.
Evicts LRU entries if over capacity.
| key | cache key |
| value | cache value (moved in) |
References currentBytes_, evictWhileOverCapacity(), list_, map_, mutex_, and sizeFunc_.
|
inline |
References evictWhileOverCapacity(), maxBytes(), maxBytes_, and mutex_.
|
inline |
|
private |
Referenced by clear(), currentBytes(), erase(), evictWhileOverCapacity(), LRUCache(), and put().
|
private |
Referenced by evictWhileOverCapacity(), and LRUCache().
|
private |
Referenced by clear(), erase(), evictWhileOverCapacity(), get(), and put().
|
private |
Referenced by clear(), contains(), erase(), evictWhileOverCapacity(), get(), put(), and size().
|
private |
Referenced by evictWhileOverCapacity(), LRUCache(), maxBytes(), and setMaxBytes().
|
mutableprivate |
Referenced by clear(), contains(), currentBytes(), erase(), get(), put(), setMaxBytes(), and size().
|
private |
Referenced by erase(), evictWhileOverCapacity(), LRUCache(), and put().