Cleanup disk cache io device

This commit is contained in:
Atlante45 2016-04-18 13:46:45 -07:00
parent b943a664b6
commit c9fa91936a

View file

@ -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);