mirror of
https://github.com/overte-org/overte.git
synced 2025-04-14 20:08:56 +02:00
Merge pull request #848 from humbletim/smarter-scriptcache
ScriptCache: add local file change detection
This commit is contained in:
commit
b091557cb5
5 changed files with 34 additions and 8 deletions
|
@ -13,6 +13,7 @@
|
||||||
|
|
||||||
#include <QtCore/QFile>
|
#include <QtCore/QFile>
|
||||||
#include <QtCore/QFileSelector>
|
#include <QtCore/QFileSelector>
|
||||||
|
#include <QtCore/QDateTime>
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
|
|
||||||
#include <StatTracker.h>
|
#include <StatTracker.h>
|
||||||
|
@ -54,6 +55,7 @@ void FileResourceRequest::doSend() {
|
||||||
} else {
|
} else {
|
||||||
QFile file(filename);
|
QFile file(filename);
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
|
setProperty("last-modified", toHttpDateString(QFileInfo(file).lastModified().toMSecsSinceEpoch()));
|
||||||
if (file.open(QFile::ReadOnly)) {
|
if (file.open(QFile::ReadOnly)) {
|
||||||
|
|
||||||
if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) {
|
if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) {
|
||||||
|
|
|
@ -15,8 +15,14 @@
|
||||||
#include <DependencyManager.h>
|
#include <DependencyManager.h>
|
||||||
#include <StatTracker.h>
|
#include <StatTracker.h>
|
||||||
|
|
||||||
|
#include <QtCore/QDateTime>
|
||||||
#include <QtCore/QThread>
|
#include <QtCore/QThread>
|
||||||
|
|
||||||
|
QString ResourceRequest::toHttpDateString(uint64_t msecsSinceEpoch) {
|
||||||
|
return QDateTime::fromMSecsSinceEpoch(msecsSinceEpoch)
|
||||||
|
.toString("ddd, dd MMM yyyy hh:mm:ss 'GMT'")
|
||||||
|
.toLatin1();
|
||||||
|
}
|
||||||
|
|
||||||
void ResourceRequest::send() {
|
void ResourceRequest::send() {
|
||||||
if (QThread::currentThread() != thread()) {
|
if (QThread::currentThread() != thread()) {
|
||||||
|
|
|
@ -90,6 +90,7 @@ public:
|
||||||
void setCacheEnabled(bool value) { _cacheEnabled = value; }
|
void setCacheEnabled(bool value) { _cacheEnabled = value; }
|
||||||
void setByteRange(ByteRange byteRange) { _byteRange = byteRange; }
|
void setByteRange(ByteRange byteRange) { _byteRange = byteRange; }
|
||||||
|
|
||||||
|
static QString toHttpDateString(uint64_t msecsSinceEpoch);
|
||||||
public slots:
|
public slots:
|
||||||
void send();
|
void send();
|
||||||
|
|
||||||
|
|
|
@ -85,11 +85,25 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable
|
||||||
|
|
||||||
Lock lock(_containerLock);
|
Lock lock(_containerLock);
|
||||||
if (_scriptCache.contains(url) && !forceDownload) {
|
if (_scriptCache.contains(url) && !forceDownload) {
|
||||||
auto scriptContent = _scriptCache[url];
|
auto entry = _scriptCache[url];
|
||||||
lock.unlock();
|
if (url.isLocalFile() || url.scheme().isEmpty()) {
|
||||||
qCDebug(scriptengine) << "Found script in cache:" << url.fileName();
|
auto modifiedTime = QFileInfo(url.toLocalFile()).lastModified();
|
||||||
contentAvailable(url.toString(), scriptContent, true, true, STATUS_CACHED);
|
QString localTime = ResourceRequest::toHttpDateString(modifiedTime.toMSecsSinceEpoch());
|
||||||
} else {
|
QString cachedTime = entry["last-modified"].toString();
|
||||||
|
if (cachedTime != localTime) {
|
||||||
|
forceDownload = true;
|
||||||
|
qCDebug(scriptengine) << "Found script in cache, but local file modified; reloading:" << url.fileName()
|
||||||
|
<< "(memory:" << cachedTime << "disk:" << localTime << ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!forceDownload) {
|
||||||
|
lock.unlock();
|
||||||
|
qCDebug(scriptengine) << "Found script in cache:" << url.fileName();
|
||||||
|
contentAvailable(url.toString(), entry["data"].toString(), true, true, STATUS_CACHED);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
{
|
||||||
auto& scriptRequest = _activeScriptRequests[url];
|
auto& scriptRequest = _activeScriptRequests[url];
|
||||||
bool alreadyWaiting = scriptRequest.scriptUsers.size() > 0;
|
bool alreadyWaiting = scriptRequest.scriptUsers.size() > 0;
|
||||||
scriptRequest.scriptUsers.push_back(contentAvailable);
|
scriptRequest.scriptUsers.push_back(contentAvailable);
|
||||||
|
@ -140,7 +154,10 @@ void ScriptCache::scriptContentAvailable(int maxRetries) {
|
||||||
|
|
||||||
_activeScriptRequests.remove(url);
|
_activeScriptRequests.remove(url);
|
||||||
|
|
||||||
_scriptCache[url] = scriptContent = req->getData();
|
_scriptCache[url] = {
|
||||||
|
{ "data", scriptContent = req->getData() },
|
||||||
|
{ "last-modified", req->property("last-modified") },
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
auto result = req->getResult();
|
auto result = req->getResult();
|
||||||
bool irrecoverable =
|
bool irrecoverable =
|
||||||
|
@ -177,7 +194,7 @@ void ScriptCache::scriptContentAvailable(int maxRetries) {
|
||||||
allCallbacks = scriptRequest.scriptUsers;
|
allCallbacks = scriptRequest.scriptUsers;
|
||||||
|
|
||||||
if (_scriptCache.contains(url)) {
|
if (_scriptCache.contains(url)) {
|
||||||
scriptContent = _scriptCache[url];
|
scriptContent = _scriptCache[url]["data"].toString();
|
||||||
}
|
}
|
||||||
_activeScriptRequests.remove(url);
|
_activeScriptRequests.remove(url);
|
||||||
qCWarning(scriptengine) << "Error loading script from URL (" << status <<")";
|
qCWarning(scriptengine) << "Error loading script from URL (" << status <<")";
|
||||||
|
|
|
@ -60,7 +60,7 @@ private:
|
||||||
Mutex _containerLock;
|
Mutex _containerLock;
|
||||||
QMap<QUrl, ScriptRequest> _activeScriptRequests;
|
QMap<QUrl, ScriptRequest> _activeScriptRequests;
|
||||||
|
|
||||||
QHash<QUrl, QString> _scriptCache;
|
QHash<QUrl, QVariantMap> _scriptCache;
|
||||||
QMultiMap<QUrl, ScriptUser*> _scriptUsers;
|
QMultiMap<QUrl, ScriptUser*> _scriptUsers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue