From 5e767a91d90d663c2c06a878b803cb815180796f Mon Sep 17 00:00:00 2001 From: flow Date: Wed, 31 Aug 2022 18:28:07 -0300 Subject: [PATCH] Merge pull request #1080 from flowln/eternal_cache Never invalidate libraries cache entries by time elapsed --- launcher/minecraft/Library.cpp | 3 +++ launcher/minecraft/update/FMLLibrariesTask.cpp | 3 ++- launcher/net/Download.cpp | 2 +- launcher/net/Download.h | 2 +- launcher/net/HttpMetaCache.cpp | 17 +++++++++++++---- launcher/net/HttpMetaCache.h | 9 ++++++++- launcher/net/MetaCacheSink.cpp | 9 ++++++--- launcher/net/MetaCacheSink.h | 3 ++- 8 files changed, 36 insertions(+), 12 deletions(-) diff --git a/launcher/minecraft/Library.cpp b/launcher/minecraft/Library.cpp index c79827057..ba7aed4b5 100644 --- a/launcher/minecraft/Library.cpp +++ b/launcher/minecraft/Library.cpp @@ -88,6 +88,9 @@ QList Library::getDownloads( options |= Net::Download::Option::AcceptLocalFiles; } + // Don't add a time limit for the libraries cache entry validity + options |= Net::Download::Option::MakeEternal; + if(sha1.size()) { auto rawSha1 = QByteArray::fromHex(sha1.toLatin1()); diff --git a/launcher/minecraft/update/FMLLibrariesTask.cpp b/launcher/minecraft/update/FMLLibrariesTask.cpp index b6238ce91..7a0bd2f32 100644 --- a/launcher/minecraft/update/FMLLibrariesTask.cpp +++ b/launcher/minecraft/update/FMLLibrariesTask.cpp @@ -63,11 +63,12 @@ void FMLLibrariesTask::executeTask() setStatus(tr("Downloading FML libraries...")); auto dljob = new NetJob("FML libraries", APPLICATION->network()); auto metacache = APPLICATION->metacache(); + Net::Download::Options options = Net::Download::Option::MakeEternal; for (auto &lib : fmlLibsToProcess) { auto entry = metacache->resolveEntry("fmllibs", lib.filename); QString urlString = BuildConfig.FMLLIBS_BASE_URL + lib.filename; - dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry)); + dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry, options)); } connect(dljob, &NetJob::succeeded, this, &FMLLibrariesTask::fmllibsFinished); diff --git a/launcher/net/Download.cpp b/launcher/net/Download.cpp index e6a6adcc1..3778b9393 100644 --- a/launcher/net/Download.cpp +++ b/launcher/net/Download.cpp @@ -60,7 +60,7 @@ auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Down dl->m_url = url; dl->m_options = options; auto md5Node = new ChecksumValidator(QCryptographicHash::Md5); - auto cachedNode = new MetaCacheSink(entry, md5Node); + auto cachedNode = new MetaCacheSink(entry, md5Node, options.testFlag(Option::MakeEternal)); dl->m_sink.reset(cachedNode); return dl; } diff --git a/launcher/net/Download.h b/launcher/net/Download.h index 1d2643819..3faa5db5b 100644 --- a/launcher/net/Download.h +++ b/launcher/net/Download.h @@ -49,7 +49,7 @@ class Download : public NetAction { public: using Ptr = shared_qobject_ptr; - enum class Option { NoOptions = 0, AcceptLocalFiles = 1 }; + enum class Option { NoOptions = 0, AcceptLocalFiles = 1, MakeEternal = 2 }; Q_DECLARE_FLAGS(Options, Option) protected: diff --git a/launcher/net/HttpMetaCache.cpp b/launcher/net/HttpMetaCache.cpp index deb2780bb..9606ddb6d 100644 --- a/launcher/net/HttpMetaCache.cpp +++ b/launcher/net/HttpMetaCache.cpp @@ -229,8 +229,13 @@ void HttpMetaCache::Load() foo->etag = Json::ensureString(element_obj, "etag"); foo->local_changed_timestamp = Json::ensureDouble(element_obj, "last_changed_timestamp"); foo->remote_changed_timestamp = Json::ensureString(element_obj, "remote_changed_timestamp"); - foo->current_age = Json::ensureDouble(element_obj, "current_age"); - foo->max_age = Json::ensureDouble(element_obj, "max_age"); + + foo->makeEternal(Json::ensureBoolean(element_obj, "eternal", false)); + if (!foo->isEternal()) { + foo->current_age = Json::ensureDouble(element_obj, "current_age"); + foo->max_age = Json::ensureDouble(element_obj, "max_age"); + } + // presumed innocent until closer examination foo->stale = false; @@ -271,8 +276,12 @@ void HttpMetaCache::SaveNow() entryObj.insert("last_changed_timestamp", QJsonValue(double(entry->local_changed_timestamp))); if (!entry->remote_changed_timestamp.isEmpty()) entryObj.insert("remote_changed_timestamp", QJsonValue(entry->remote_changed_timestamp)); - entryObj.insert("current_age", QJsonValue(double(entry->current_age))); - entryObj.insert("max_age", QJsonValue(double(entry->max_age))); + if (entry->isEternal()) { + entryObj.insert("eternal", true); + } else { + entryObj.insert("current_age", QJsonValue(double(entry->current_age))); + entryObj.insert("max_age", QJsonValue(double(entry->max_age))); + } entriesArr.append(entryObj); } } diff --git a/launcher/net/HttpMetaCache.h b/launcher/net/HttpMetaCache.h index df3549e82..c0b123185 100644 --- a/launcher/net/HttpMetaCache.h +++ b/launcher/net/HttpMetaCache.h @@ -64,13 +64,17 @@ class MetaEntry { auto getMD5Sum() -> QString { return md5sum; } void setMD5Sum(QString md5sum) { this->md5sum = md5sum; } + /* Whether the entry expires after some time (false) or not (true). */ + void makeEternal(bool eternal) { is_eternal = eternal; } + [[nodiscard]] bool isEternal() const { return is_eternal; } + auto getCurrentAge() -> qint64 { return current_age; } void setCurrentAge(qint64 age) { current_age = age; } auto getMaximumAge() -> qint64 { return max_age; } void setMaximumAge(qint64 age) { max_age = age; } - bool isExpired(qint64 offset) { return current_age >= max_age - offset; }; + bool isExpired(qint64 offset) { return !is_eternal && (current_age >= max_age - offset); }; protected: QString baseId; @@ -78,10 +82,13 @@ class MetaEntry { QString relativePath; QString md5sum; QString etag; + qint64 local_changed_timestamp = 0; QString remote_changed_timestamp; // QString for now, RFC 2822 encoded time qint64 current_age = 0; qint64 max_age = 0; + bool is_eternal = false; + bool stale = true; }; diff --git a/launcher/net/MetaCacheSink.cpp b/launcher/net/MetaCacheSink.cpp index ab0c9fcb5..5ae53c1c5 100644 --- a/launcher/net/MetaCacheSink.cpp +++ b/launcher/net/MetaCacheSink.cpp @@ -46,8 +46,8 @@ namespace Net { #define MAX_TIME_TO_EXPIRE 1*7*24*60*60 -MetaCacheSink::MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum) - :Net::FileSink(entry->getFullPath()), m_entry(entry), m_md5Node(md5sum) +MetaCacheSink::MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum, bool is_eternal) + :Net::FileSink(entry->getFullPath()), m_entry(entry), m_md5Node(md5sum), m_is_eternal(is_eternal) { addValidator(md5sum); } @@ -95,7 +95,10 @@ Task::State MetaCacheSink::finalizeCache(QNetworkReply & reply) m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch()); { // Cache lifetime - if (reply.hasRawHeader("Cache-Control")) { + if (m_is_eternal) { + qDebug() << "[MetaCache] Adding eternal cache entry:" << m_entry->getFullPath(); + m_entry->makeEternal(true); + } else if (reply.hasRawHeader("Cache-Control")) { auto cache_control_header = reply.rawHeader("Cache-Control"); // qDebug() << "[MetaCache] Parsing 'Cache-Control' header with" << cache_control_header; diff --git a/launcher/net/MetaCacheSink.h b/launcher/net/MetaCacheSink.h index c9f7edfe7..f59480857 100644 --- a/launcher/net/MetaCacheSink.h +++ b/launcher/net/MetaCacheSink.h @@ -42,7 +42,7 @@ namespace Net { class MetaCacheSink : public FileSink { public: - MetaCacheSink(MetaEntryPtr entry, ChecksumValidator* md5sum); + MetaCacheSink(MetaEntryPtr entry, ChecksumValidator* md5sum, bool is_eternal = false); virtual ~MetaCacheSink() = default; auto hasLocalData() -> bool override; @@ -54,5 +54,6 @@ class MetaCacheSink : public FileSink { private: MetaEntryPtr m_entry; ChecksumValidator* m_md5Node; + bool m_is_eternal; }; } // namespace Net