From ec87a8ddfc2e6c23ab4c1003f6cafa685ce233d8 Mon Sep 17 00:00:00 2001 From: flow Date: Sat, 16 Jul 2022 20:29:03 -0300 Subject: [PATCH] fix: add expiration time to cache entries This is to prevent problems where the cache entry would still be used way after the remote resource got updated. The limit is hardcoded for 1 week, which I think is a reasonable time, but this could be further tweaked. Signed-off-by: flow --- launcher/net/HttpMetaCache.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/launcher/net/HttpMetaCache.cpp b/launcher/net/HttpMetaCache.cpp index 4d86c0b8..769f162b 100644 --- a/launcher/net/HttpMetaCache.cpp +++ b/launcher/net/HttpMetaCache.cpp @@ -44,6 +44,11 @@ #include +/** Maximum time to hold a cache entry + * = 1 week in milliseconds + */ +#define TIME_TO_EXPIRE 1*7*24*60*60*1000 + auto MetaEntry::getFullPath() -> QString { // FIXME: make local? @@ -121,6 +126,15 @@ auto HttpMetaCache::resolveEntry(QString base, QString resource_path, QString ex SaveEventually(); } + // Get rid of old entries, to prevent cache problems + auto current_time = QDateTime::currentMSecsSinceEpoch(); + auto remote_time = QDateTime::fromString(entry->remote_changed_timestamp).toMSecsSinceEpoch(); + if (current_time - remote_time < TIME_TO_EXPIRE) { + qWarning() << "Removing cache entry because of old age!"; + selected_base.entry_list.remove(resource_path); + return staleEntry(base, resource_path); + } + // entry passed all the checks we cared about. entry->basePath = getBasePath(base); return entry; @@ -240,6 +254,8 @@ void HttpMetaCache::SaveNow() if (m_index_file.isNull()) return; + qDebug() << "[HttpMetaCache]" << "Saving metacache with" << m_entries.size() << "entries"; + QJsonObject toplevel; Json::writeString(toplevel, "version", "1");