D++ (DPP)
C++ Discord API Bot Library
|
A cache object maintains a cache of dpp::managed objects. More...
#include <cache.h>
Public Member Functions | |
cache () | |
Construct a new cache object. More... | |
~cache () | |
Destroy the cache object. More... | |
void | store (T *object) |
Store an object in the cache. Passing a nullptr will have no effect. More... | |
void | remove (T *object) |
Remove an object from the cache. More... | |
T * | find (snowflake id) |
Find an object in the cache by id. More... | |
uint64_t | count () |
Return a count of the number of items in the cache. More... | |
std::shared_mutex & | get_mutex () |
Return the cache's locking mutex. More... | |
auto & | get_container () |
Get the container unordered map. More... | |
void | rehash () |
"Rehash" a cache by reallocating the map and copying all elements into the new one. More... | |
size_t | bytes () |
Get "real" size in RAM of the cached objects. More... | |
A cache object maintains a cache of dpp::managed objects.
This is for example users, channels or guilds. You may instantiate your own caches, to contain any type derived from dpp::managed including your own types.
T | class type to store, which should be derived from dpp::managed. |
|
inline |
Construct a new cache object.
Caches must contain classes derived from dpp::managed.
|
inline |
Destroy the cache object.
|
inline |
Get "real" size in RAM of the cached objects.
This does not include metadata used to maintain the undordered map itself.
|
inline |
Return a count of the number of items in the cache.
This is used by the library e.g. to count guilds, users, and roles stored within caches. get
|
inline |
Find an object in the cache by id.
The cache is searched for the object. All dpp::managed objects have a snowflake id (this is the only field dpp::managed actually has).
id | Object snowflake id to find |
|
inline |
Get the container unordered map.
|
inline |
Return the cache's locking mutex.
Use this whenever you manipulate or iterate raw elements in the cache!
std::shared_lock
, else wrap it in a std::unique_lock
. Shared locks will allow for multiple readers whilst blocking writers, and unique locks will allow only one writer whilst blocking readers and writers.Example:
|
inline |
"Rehash" a cache by reallocating the map and copying all elements into the new one.
Over a long running timeframe, unordered maps can grow in size due to bucket allocation, this function frees that unused memory to keep the maps in control over time. If this is an issue which is apparent with your use of dpp::cache objects, you should periodically call this method.
|
inline |
Remove an object from the cache.
object | object to remove. Passing a nullptr will have no effect. |
|
inline |
Store an object in the cache. Passing a nullptr will have no effect.
The object must be derived from dpp::managed and should be allocated on the heap. Generally this is done via new
. Once stored in the cache the lifetime of the stored object is managed by the cache class unless the cache is deleted (at which point responsibility for deleting the object returns to its allocator). Objects stored are removed when the cache::remove() method is called by placing them into a garbage collection queue for deletion within the next 60 seconds, which are then deleted in bulk for efficiency and to aid thread safety.
object | object to store. Storing a pointer to the cache relinquishes ownership to the cache object. |