GenericDataCache Class

template <typename T> class QInstaller::GenericDataCache

The GenericDataCache is a template class for a checksum based storage of items on disk. More...

Header: #include <GenericDataCache>

Public Functions

GenericDataCache(const QString &path, const QString &type, const QString &version)
bool clear()
QString errorString() const
bool initialize()
bool isValid() const
T *itemByChecksum(const QByteArray &checksum) const
T *itemByPath(const QString &path) const
QList<T *> items() const
QList<T *> obsoleteItems() const
QString path() const
bool registerItem(T *item, bool replace = false)
bool removeItem(const QByteArray &checksum)
void setPath(const QString &path)
void setType(const QString &type)
void setVersion(const QString &version)
bool sync()

Detailed Description

GenericDataCache<T> manages a cache storage for a set path(), which contains a subdirectory for each registered item. An item of type T should implement methods declared in the CacheableItem interface. The GenericDataCache<T> class can still be explicitly specialized to use the derived type as a template argument, to allow retrieving items as the derived type without casting.

Each cache has a manifest file in its root directory, which lists the version and wrapped type of the cache, and all its items. The file is updated automatically when the cache object is destructed, or it can be updated periodically by calling sync().

Member Function Documentation

GenericDataCache::GenericDataCache(const QString &path, const QString &type, const QString &version)

Constructs a cache to path with the given type and version. The cache is initialized automatically.


Constructs a new empty cache. The cache is invalid until set with a path and initialized.


Deletes the cache object. Item contents on disk are kept.

bool GenericDataCache::clear()

Removes all items from the cache and deletes their contents on disk. If the cache directory becomes empty, it is also deleted. The cache becomes invalid after this action, even in case of error while clearing. In that case already deleted items will be lost. Returns true on success, false otherwise.

QString GenericDataCache::errorString() const

Returns a string representing the last error with the cache.

bool GenericDataCache::initialize()

Initializes a cache. Creates a new directory for the path configured for this cache if it does not exist, and loads any previously cached items from the directory. The cache directory is locked for access by this process only. Returns true on success, false otherwise.

bool GenericDataCache::isValid() const

Returns true if the cache is valid, false otherwise. A cache is considered valid when it is initialized to a set path.

T *GenericDataCache::itemByChecksum(const QByteArray &checksum) const

Returns an item that matches the checksum or nullptr in case no such item is cached.

T *GenericDataCache::itemByPath(const QString &path) const

Returns an item from the path or nullptr in case no such item is cached. Depending on the size of the cache, this can be much slower than retrieving an item with itemByChecksum().

QList<T *> GenericDataCache::items() const

Returns a list of cached items.

QList<T *> GenericDataCache::obsoleteItems() const

Returns items considered obsolete from the cache.

QString GenericDataCache::path() const

Returns the path of the cache on disk.

See also setPath().

bool GenericDataCache::registerItem(T *item, bool replace = false)

Registers the item to the cache. If replace is set to true, the new item replaces a previous item with the same checksum.

The cache takes ownership of the object pointed by item. The contents of the item are copied to the cache with a subdirectory name that matches the checksum of the item.

Returns true on success or false if the item could not be registered.

bool GenericDataCache::removeItem(const QByteArray &checksum)

Removes the item specified by checksum from the cache and deletes the contents of the item from disk. Returns true if the item was removed successfully, false otherwise.

void GenericDataCache::setPath(const QString &path)

Sets a new path for the cache and invalidates current items. Saves the information of the old cache to its manifest file.

See also path().

void GenericDataCache::setType(const QString &type)

Sets the name of the wrapped type to type. This is used for determining if an existing cache holds items of the same type. Trying to load cached items with mismatching type results in discarding the old items. Optional.

void GenericDataCache::setVersion(const QString &version)

Sets the version of the cache to version. Loading from a cache with different expected version discards the old items. The version property defaults to 1.0.0.

bool GenericDataCache::sync()

Synchronizes the contents of the cache to its manifest file. Returns true if the manifest file was updates successfully, false otherwise.

© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. The Qt Company, Qt and their respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.