Add mechanism for temporarily disabling all network caching

to QgsNetworkAccessManager, and add a checkbox in the network
logger panel to allow users to temporarily disable the network cache

This can be VERY useful when debugging QGIS network activity, or when
using QGIS to test server side changes.

This is a transient setting only, and is forgotten as soon as QGIS
is closed. That's by design -- we don't want users to accidentally
leave this enabled and cause unnecessary server load.
This commit is contained in:
Nyall Dawson 2021-01-19 16:12:43 +10:00
parent 0b2c951e65
commit 36f68e4fc9
4 changed files with 49 additions and 0 deletions

View File

@ -250,6 +250,7 @@ need to be handled on the main thread. See in-depth discussion in the documentat
for the constructor of this class.
%End
bool useSystemProxy() const;
%Docstring
Returns whether the system proxy should be used.

View File

@ -27,6 +27,7 @@
#include <QMessageBox>
#include <QScrollBar>
#include <QToolButton>
#include <QCheckBox>
#include <nlohmann/json.hpp>
@ -217,4 +218,16 @@ QgsNetworkLoggerPanelWidget::QgsNetworkLoggerPanelWidget( QgsNetworkLogger *logg
settingsMenu->addAction( mActionShowSuccessful );
settingsMenu->addAction( mActionShowTimeouts );
mToolbar->addSeparator();
QCheckBox *disableCacheCheck = new QCheckBox( tr( "Disable cache" ) );
connect( disableCacheCheck, &QCheckBox::toggled, this, [ = ]( bool checked )
{
// note -- we deliberately do NOT store this as a permanent setting in QSettings
// as it is designed to be a temporary debugging tool only and we don't want
// users to accidentally leave this enabled and cause unnecessary server load...
QgsNetworkAccessManager::instance()->setCacheDisabled( checked );
} );
mToolbar->addWidget( disableCacheCheck );
}

View File

@ -127,7 +127,10 @@ QgsNetworkAccessManager *QgsNetworkAccessManager::instance( Qt::ConnectionType c
sMainNAM = nam;
if ( !nam->mInitialized )
{
nam->setupDefaultProxyAndCache( connectionType );
nam->setCacheDisabled( sMainNAM->cacheDisabled() );
}
return nam;
}
@ -249,6 +252,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
}
#endif
if ( sMainNAM->mCacheDisabled )
{
// if caching is disabled then we override whatever the request actually has set!
pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
}
static QAtomicInt sRequestId = 0;
const int requestId = ++sRequestId;
QByteArray content;

View File

@ -409,6 +409,30 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
#ifndef SIP_RUN
/**
* Returns TRUE if all network caching is disabled.
*
* \see setCacheDisabled()
* \note Not available in Python bindings.
* \since QGIS 3.18
*/
bool cacheDisabled() const { return mCacheDisabled; }
/**
* Sets whether all network caching should be disabled.
*
* If \a disabled is TRUE then all caching will be disabled, causing all requests
* to be retrieved from the network regardless of the request's attributes.
*
* \see cacheDisabled()
* \note Not available in Python bindings.
* \since QGIS 3.18
*/
void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
#endif
/**
* Returns whether the system proxy should be used.
*/
@ -655,6 +679,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
QStringList mNoProxyURLs;
bool mUseSystemProxy = false;
bool mInitialized = false;
bool mCacheDisabled = false;
static QgsNetworkAccessManager *sMainNAM;
// ssl error handler, will be set for main thread ONLY
std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;