mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-25 17:14:59 +02:00
Cleanup disk cache io device
This commit is contained in:
parent
b943a664b6
commit
c9fa91936a
1 changed files with 8 additions and 2 deletions
|
@ -11,6 +11,8 @@
|
|||
|
||||
#include "AssetUtils.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QtCore/QCryptographicHash>
|
||||
#include <QtNetwork/QAbstractNetworkCache>
|
||||
|
||||
|
@ -29,12 +31,15 @@ QByteArray hashData(const QByteArray& data) {
|
|||
|
||||
QByteArray loadFromCache(const QUrl& url) {
|
||||
if (auto cache = NetworkAccessManager::getInstance().cache()) {
|
||||
if (auto ioDevice = cache->data(url)) {
|
||||
|
||||
// caller is responsible for the deletion of the ioDevice, hence the unique_ptr
|
||||
if (auto ioDevice = std::unique_ptr<QIODevice>(cache->data(url))) {
|
||||
qCDebug(asset_client) << url.toDisplayString() << "loaded from disk cache.";
|
||||
return ioDevice->readAll();
|
||||
} else {
|
||||
qCDebug(asset_client) << url.toDisplayString() << "not in disk cache";
|
||||
}
|
||||
|
||||
} else {
|
||||
qCWarning(asset_client) << "No disk cache to load assets from.";
|
||||
}
|
||||
|
@ -49,7 +54,8 @@ bool saveToCache(const QUrl& url, const QByteArray& file) {
|
|||
metaData.setSaveToDisk(true);
|
||||
metaData.setLastModified(QDateTime::currentDateTime());
|
||||
metaData.setExpirationDate(QDateTime()); // Never expires
|
||||
|
||||
|
||||
// ioDevice is managed by the cache and should either be passed back to insert or remove!
|
||||
if (auto ioDevice = cache->prepare(metaData)) {
|
||||
ioDevice->write(file);
|
||||
cache->insert(ioDevice);
|
||||
|
|
Loading…
Reference in a new issue