GenericDataCache Class | Qt Installer Framework Manual

- Qt Installer Framework Manual
- GenericDataCache

# 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>
Inherited By: | QInstaller::MetadataCache

- List of all members, including inherited members

## Public Types

enum | RegisterMode { Copy, Move }

## Public Functions

| GenericDataCache()
| GenericDataCache(const QString &path, const QString &type, const QString &version)
virtual | ~GenericDataCache()
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, QInstaller::GenericDataCache::RegisterMode mode = Copy)
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 Type Documentation

### enum GenericDataCache::RegisterMode

This enum holds the possible values for modes of registering items to cache.

Constant | Value | Description
--- | --- | ---
`QInstaller::GenericDataCache::Copy` | `0` | The contents of the item are copied to the cache.
`QInstaller::GenericDataCache::Move` | `1` | The contents of the item are move to the cache.

## Member Function Documentation

### GenericDataCache::GenericDataCache()

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

### `[explicit]` 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.

### `[virtual]` GenericDataCache::~GenericDataCache()

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, QInstaller::GenericDataCache::RegisterMode mode = Copy)

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 or moved to the cache with a subdirectory name that matches the checksum of the item. The mode decides how the contents of the item are registered, either by copying or moving.

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.