An asynchronous caching data wrapper. More...
#include <cached_type_async.h>
Public Member Functions | |
MemoryUsage::MemorySize | allocSize () const |
allocSize() More... | |
AsyncCached () | |
Default ctor, only valid if Type is default-constructible. More... | |
template<typename... Args, typename std::enable_if<!std::is_same< typename std::decay< Args... >::type, typename std::decay< AsyncCached< Type > >::type >::value, int >::type = 0> | |
AsyncCached (Args &&... _args) | |
Direct ctor for the contained type. More... | |
AsyncCached (AsyncCached &&other) | |
Move ctor. More... | |
AsyncCached (AsyncCached const &other) | |
Copy ctor. More... | |
AsyncCached (Type &&_data) | |
Ctor from an object of the contained type. More... | |
void | cache () const |
Caches the object to disk and deletes the in-memory copy. More... | |
void | cache_async () const |
Caches the object to disk asynchronously and deletes the in-memory copy eventually. More... | |
MemoryUsage::MemorySize | cacheSize () const |
Memory which is currently free but would be needed for uncaching. More... | |
void | delete_file () const |
Deletes the cache file if it exists and empties the file string. More... | |
bool | is_data_loaded () const |
Getter for data_is_loaded . More... | |
bool | is_file_valid () const |
Getter for file_is_valid . More... | |
bool | is_worker_running () const |
Getter for worker_is_running . More... | |
void | load () const |
Loads the object from disk but does not delete the disk file. More... | |
template<typename... Args> | |
void | log (Args &&... args) const |
Logging helper. More... | |
void | maybe_cache (bool do_it) const |
Caches the object if do_it is true. More... | |
void | maybe_cache_async (bool do_it) const |
Caches the object asynchronously if do_it is true. More... | |
void | maybeCache (bool do_it) const |
Alias for maybe_cache() More... | |
Type & | operator() () |
Returns a reference to the object. More... | |
Type const & | operator() () const |
Returns a const reference to the object. More... | |
Type const & | operator() (int) const |
Returns a const reference to the object. More... | |
AsyncCached & | operator= (AsyncCached &&other) |
Move assignment operator. More... | |
AsyncCached & | operator= (AsyncCached const &other) |
Copy assignment operator. More... | |
void | uncache () const |
Alias for load() . More... | |
std::unique_lock< std::recursive_mutex > | wait_for_worker_completion () const |
Waits until any currently running worker job has completed and then returns a lock with locked mutex to the caller, ensuring that no further worker jobs can start. More... | |
~AsyncCached () | |
Dtor. More... | |
Boost serialisation | |
template<class Archive > | |
void | save (Archive &ar, const unsigned int) const |
Saving into a boost archive. More... | |
template<class Archive > | |
void | load (Archive &ar, const unsigned int in_version) const |
Static Public Attributes | |
static constexpr unsigned int | version = 1 |
Boost serialisation version. More... | |
Private Attributes | |
std::size_t | alloc_size_buffer = 0 |
Caches the amount of memory needed to uncache. More... | |
Type | data |
potential storage space for the object. More... | |
bool | data_is_loaded {false} |
decides whether data holds the correct representation of our object. More... | |
std::string | file |
potential file name of the cached file More... | |
bool | file_is_valid {false} |
decides whether file holds the file name of a correct representation of our object. More... | |
std::recursive_mutex | mtex |
mutex protecting all other members More... | |
std::thread | worker |
worker to be launched for asynchronous caching More... | |
bool | worker_is_running {false} |
decides whether the worker is currently running. More... | |
An asynchronous caching data wrapper.
AsyncCached behaves much the same way as Cached except that it also exposes a cache_async()
function. If called, this function typically launches a worker thread to do the actual caching and immediately returns. This means that storing data to disk can be done asynchronously.
Additionally, the type differentiates between mutable and const references and only deletes an underlying file if a mutable reference to the contained object is given. That is, if the object is retrieved from cache and then only accessed via a const reference, a call to cache()
(or cache_async()
) will cause the object to be cleared from memory without additionally re-writing the same data to disk.
Using this class only makes sense for types whose default-constructed objects are small (e.g. std::vector
). It does not make sense to use this class for primarily stack-allocating types (e.g. std::array
).