mirror of
https://github.com/overte-org/overte.git
synced 2025-04-07 23:53:54 +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/QFileSelector>
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
#include <StatTracker.h>
|
||||
|
@ -54,6 +55,7 @@ void FileResourceRequest::doSend() {
|
|||
} else {
|
||||
QFile file(filename);
|
||||
if (file.exists()) {
|
||||
setProperty("last-modified", toHttpDateString(QFileInfo(file).lastModified().toMSecsSinceEpoch()));
|
||||
if (file.open(QFile::ReadOnly)) {
|
||||
|
||||
if (file.size() < _byteRange.fromInclusive || file.size() < _byteRange.toExclusive) {
|
||||
|
|
|
@ -15,8 +15,14 @@
|
|||
#include <DependencyManager.h>
|
||||
#include <StatTracker.h>
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#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() {
|
||||
if (QThread::currentThread() != thread()) {
|
||||
|
|
|
@ -90,6 +90,7 @@ public:
|
|||
void setCacheEnabled(bool value) { _cacheEnabled = value; }
|
||||
void setByteRange(ByteRange byteRange) { _byteRange = byteRange; }
|
||||
|
||||
static QString toHttpDateString(uint64_t msecsSinceEpoch);
|
||||
public slots:
|
||||
void send();
|
||||
|
||||
|
|
|
@ -85,11 +85,25 @@ void ScriptCache::getScriptContents(const QString& scriptOrURL, contentAvailable
|
|||
|
||||
Lock lock(_containerLock);
|
||||
if (_scriptCache.contains(url) && !forceDownload) {
|
||||
auto scriptContent = _scriptCache[url];
|
||||
lock.unlock();
|
||||
qCDebug(scriptengine) << "Found script in cache:" << url.fileName();
|
||||
contentAvailable(url.toString(), scriptContent, true, true, STATUS_CACHED);
|
||||
} else {
|
||||
auto entry = _scriptCache[url];
|
||||
if (url.isLocalFile() || url.scheme().isEmpty()) {
|
||||
auto modifiedTime = QFileInfo(url.toLocalFile()).lastModified();
|
||||
QString localTime = ResourceRequest::toHttpDateString(modifiedTime.toMSecsSinceEpoch());
|
||||
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];
|
||||
bool alreadyWaiting = scriptRequest.scriptUsers.size() > 0;
|
||||
scriptRequest.scriptUsers.push_back(contentAvailable);
|
||||
|
@ -140,7 +154,10 @@ void ScriptCache::scriptContentAvailable(int maxRetries) {
|
|||
|
||||
_activeScriptRequests.remove(url);
|
||||
|
||||
_scriptCache[url] = scriptContent = req->getData();
|
||||
_scriptCache[url] = {
|
||||
{ "data", scriptContent = req->getData() },
|
||||
{ "last-modified", req->property("last-modified") },
|
||||
};
|
||||
} else {
|
||||
auto result = req->getResult();
|
||||
bool irrecoverable =
|
||||
|
@ -177,7 +194,7 @@ void ScriptCache::scriptContentAvailable(int maxRetries) {
|
|||
allCallbacks = scriptRequest.scriptUsers;
|
||||
|
||||
if (_scriptCache.contains(url)) {
|
||||
scriptContent = _scriptCache[url];
|
||||
scriptContent = _scriptCache[url]["data"].toString();
|
||||
}
|
||||
_activeScriptRequests.remove(url);
|
||||
qCWarning(scriptengine) << "Error loading script from URL (" << status <<")";
|
||||
|
|
|
@ -60,7 +60,7 @@ private:
|
|||
Mutex _containerLock;
|
||||
QMap<QUrl, ScriptRequest> _activeScriptRequests;
|
||||
|
||||
QHash<QUrl, QString> _scriptCache;
|
||||
QHash<QUrl, QVariantMap> _scriptCache;
|
||||
QMultiMap<QUrl, ScriptUser*> _scriptUsers;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue