Merge pull request #7700 from Atlante45/fix/cleanup-iodevice

Cleanup disk cache io device
This commit is contained in:
Seth Alves 2016-04-18 14:28:41 -07:00
commit 65f51322fa

View file

@ -11,6 +11,8 @@
#include "AssetUtils.h" #include "AssetUtils.h"
#include <memory>
#include <QtCore/QCryptographicHash> #include <QtCore/QCryptographicHash>
#include <QtNetwork/QAbstractNetworkCache> #include <QtNetwork/QAbstractNetworkCache>
@ -29,12 +31,15 @@ QByteArray hashData(const QByteArray& data) {
QByteArray loadFromCache(const QUrl& url) { QByteArray loadFromCache(const QUrl& url) {
if (auto cache = NetworkAccessManager::getInstance().cache()) { 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."; qCDebug(asset_client) << url.toDisplayString() << "loaded from disk cache.";
return ioDevice->readAll(); return ioDevice->readAll();
} else { } else {
qCDebug(asset_client) << url.toDisplayString() << "not in disk cache"; qCDebug(asset_client) << url.toDisplayString() << "not in disk cache";
} }
} else { } else {
qCWarning(asset_client) << "No disk cache to load assets from."; 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.setSaveToDisk(true);
metaData.setLastModified(QDateTime::currentDateTime()); metaData.setLastModified(QDateTime::currentDateTime());
metaData.setExpirationDate(QDateTime()); // Never expires 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)) { if (auto ioDevice = cache->prepare(metaData)) {
ioDevice->write(file); ioDevice->write(file);
cache->insert(ioDevice); cache->insert(ioDevice);