From 346dafd3a0e19805f77147a9515a7333a05ed584 Mon Sep 17 00:00:00 2001 From: Dan Leinir Turthra Jensen Date: Tue, 8 Feb 2022 11:16:16 +0000 Subject: [PATCH] Add a granular cache preference thing to attica-kde Commit 798027fd17ab86eea022e5d269aa86138b28b130 This allows us to explicitly add cache preference to specific calls. Usually attica calls want to be very current, but in some cases we can assume things aren't going to change often (such as the categories call), and we can just use the cached version for those calls. Fix missing variable name Commit 8e164050c594855ee3728678d6d0907a10b28db6 Only trust the expiration date if it's less than 24 hours Commit b85cf34298c274b5f16cb6c2aead7b87f0dabbb8 Fix build Commit 6e8626f2d65a03af3cd12fb5390040c56ca3258d --- attica-kde/kdeplugin/kdeplatformdependent.cpp | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/attica-kde/kdeplugin/kdeplatformdependent.cpp b/attica-kde/kdeplugin/kdeplatformdependent.cpp index c2c90107e..850c91284 100644 --- a/attica-kde/kdeplugin/kdeplatformdependent.cpp +++ b/attica-kde/kdeplugin/kdeplatformdependent.cpp @@ -125,6 +125,25 @@ QNetworkRequest KdePlatformDependent::addOAuthToRequest(const QNetworkRequest &r const QString bearer = bearer_format.arg(token); notConstReq.setRawHeader("Authorization", bearer.toUtf8()); } + + // Add cache preference in a granular fashion (we will almost certainly want more of these, but...) + static const QStringList preferCacheEndpoints{QLatin1String{"/content/categories"}}; + for (const QString &endpoint : preferCacheEndpoints) { + if (notConstReq.url().toString().endsWith(endpoint)) { + QNetworkCacheMetaData cacheMeta{m_accessManager->cache()->metaData(notConstReq.url())}; + if (cacheMeta.isValid()) { + // If the expiration date is valid, but longer than 24 hours, don't trust that things + // haven't changed and check first, otherwise just use the cached version to relieve + // server strain and reduce network traffic. + const QDateTime tomorrow{QDateTime::currentDateTime().addDays(1)}; + if (cacheMeta.expirationDate().isValid() && cacheMeta.expirationDate() < tomorrow) { + notConstReq.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); + } + } + break; + } + } + return notConstReq; } -- 2.34.1