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 <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-07-16 20:29:03 -03:00
parent dce435c882
commit ec87a8ddfc
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469

View File

@ -44,6 +44,11 @@
#include <QDebug>
/** 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");