From 90edf055f74ae36244c1a02c3119d61c7dfe6eae Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Thu, 9 Jul 2015 18:00:07 -0700 Subject: [PATCH] Clear unused resources before refresh --- libraries/networking/src/ResourceCache.cpp | 39 +++++++++++++--------- libraries/networking/src/ResourceCache.h | 17 ++++------ 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index cee0dbecf6..f3ed60c281 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -34,20 +34,17 @@ ResourceCache::ResourceCache(QObject* parent) : } ResourceCache::~ResourceCache() { - // the unused resources may themselves reference resources that will be added to the unused - // list on destruction, so keep clearing until there are no references left - while (!_unusedResources.isEmpty()) { - foreach (const QSharedPointer& resource, _unusedResources) { - resource->setCache(nullptr); - } - _unusedResources.clear(); - } + clearUnusedResource(); } void ResourceCache::refreshAll() { - for (auto it = _resources.begin(); it != _resources.end(); ++it) { - if (!it.value().isNull()) { - it.value().data()->refresh(); + // Clear all unused resources so we don't have to reload them + clearUnusedResource(); + + // Refresh all remaining resources in use + foreach (auto resource, _resources) { + if (!resource.isNull()) { + resource.data()->refresh(); } } } @@ -144,6 +141,17 @@ void ResourceCache::reserveUnusedResource(qint64 resourceSize) { } } +void ResourceCache::clearUnusedResource() { + // the unused resources may themselves reference resources that will be added to the unused + // list on destruction, so keep clearing until there are no references left + while (!_unusedResources.isEmpty()) { + foreach (const QSharedPointer& resource, _unusedResources) { + resource->setCache(nullptr); + } + _unusedResources.clear(); + } +} + void ResourceCache::attemptRequest(Resource* resource) { auto sharedItems = DependencyManager::get(); if (_requestLimit <= 0) { @@ -271,11 +279,12 @@ void Resource::refresh() { } void Resource::allReferencesCleared() { - if (QThread::currentThread() != thread()) { - QMetaObject::invokeMethod(this, "allReferencesCleared"); - return; - } if (_cache) { + if (QThread::currentThread() != thread()) { + QMetaObject::invokeMethod(this, "allReferencesCleared"); + return; + } + // create and reinsert new shared pointer QSharedPointer self(this, &Resource::allReferencesCleared); setSelf(self); diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h index b4b9f089c5..4997826870 100644 --- a/libraries/networking/src/ResourceCache.h +++ b/libraries/networking/src/ResourceCache.h @@ -51,7 +51,7 @@ static const qint64 MAX_UNUSED_MAX_SIZE = 10 * BYTES_PER_GIGABYTES; class ResourceCacheSharedItems : public Dependency { SINGLETON_DEPENDENCY public: - QList > _pendingRequests; + QList> _pendingRequests; QList _loadingRequests; private: ResourceCacheSharedItems() { } @@ -86,10 +86,6 @@ public slots: void checkAsynchronousGets(); protected: - qint64 _unusedResourcesMaxSize = DEFAULT_UNUSED_MAX_SIZE; - qint64 _unusedResourcesSize = 0; - QMap > _unusedResources; - /// Loads a resource from the specified URL. /// \param fallback a fallback URL to load if the desired one is unavailable /// \param delayLoad if true, don't load the resource immediately; wait until load is first requested @@ -104,6 +100,7 @@ protected: void addUnusedResource(const QSharedPointer& resource); void removeUnusedResource(const QSharedPointer& resource); void reserveUnusedResource(qint64 resourceSize); + void clearUnusedResource(); static void attemptRequest(Resource* resource); static void requestCompleted(Resource* resource); @@ -119,7 +116,10 @@ private: void getResourceAsynchronously(const QUrl& url); QReadWriteLock _resourcesToBeGottenLock; QQueue _resourcesToBeGotten; - + + qint64 _unusedResourcesMaxSize = DEFAULT_UNUSED_MAX_SIZE; + qint64 _unusedResourcesSize = 0; + QMap> _unusedResources; }; /// Base class for resources. @@ -173,12 +173,10 @@ public: const QUrl& getURL() const { return _url; } signals: - /// Fired when the resource has been loaded. void loaded(); protected slots: - void attemptRequest(); /// Refreshes the resource if the last modified date on the network @@ -186,7 +184,6 @@ protected slots: void maybeRefresh(); protected: - virtual void init(); /// Called when the download has finished. The recipient should delete the reply when done with it. @@ -208,14 +205,12 @@ protected: QPointer _cache; private slots: - void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); void handleReplyError(); void handleReplyFinished(); void handleReplyTimeout(); private: - void setLRUKey(int lruKey) { _lruKey = lruKey; } void makeRequest();