change(cache): use cache-specific http headers for their lifetime

This uses the 'Age', 'Cache-Control' and 'Expires' HTTP headers to more
accurately set up the cache lifetime, falling back to a static 1-week
time if they're not present in the response.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow
2022-07-17 11:24:12 -03:00
parent c8a72c876d
commit ab6e1b112b
3 changed files with 53 additions and 9 deletions

View File

@ -44,11 +44,6 @@
#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?
@ -127,9 +122,8 @@ auto HttpMetaCache::resolveEntry(QString base, QString resource_path, QString ex
}
// 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) {
auto current_time = QDateTime::currentSecsSinceEpoch();
if (entry->isExpired(current_time - ( file_last_changed / 1000 ))) {
qWarning() << "Removing cache entry because of old age!";
selected_base.entry_list.remove(resource_path);
return staleEntry(base, resource_path);
@ -235,6 +229,8 @@ 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");
// presumed innocent until closer examination
foo->stale = false;
@ -275,6 +271,8 @@ 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)));
entriesArr.append(entryObj);
}
}