Skip to content

Commit 36f68e4

Browse files
committedJan 19, 2021
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.
1 parent 0b2c951 commit 36f68e4

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed
 

‎python/core/auto_generated/network/qgsnetworkaccessmanager.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ need to be handled on the main thread. See in-depth discussion in the documentat
250250
for the constructor of this class.
251251
%End
252252

253+
253254
bool useSystemProxy() const;
254255
%Docstring
255256
Returns whether the system proxy should be used.

‎src/app/devtools/networklogger/qgsnetworkloggerpanelwidget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <QMessageBox>
2828
#include <QScrollBar>
2929
#include <QToolButton>
30+
#include <QCheckBox>
3031

3132
#include <nlohmann/json.hpp>
3233

@@ -217,4 +218,16 @@ QgsNetworkLoggerPanelWidget::QgsNetworkLoggerPanelWidget( QgsNetworkLogger *logg
217218

218219
settingsMenu->addAction( mActionShowSuccessful );
219220
settingsMenu->addAction( mActionShowTimeouts );
221+
222+
mToolbar->addSeparator();
223+
QCheckBox *disableCacheCheck = new QCheckBox( tr( "Disable cache" ) );
224+
connect( disableCacheCheck, &QCheckBox::toggled, this, [ = ]( bool checked )
225+
{
226+
// note -- we deliberately do NOT store this as a permanent setting in QSettings
227+
// as it is designed to be a temporary debugging tool only and we don't want
228+
// users to accidentally leave this enabled and cause unnecessary server load...
229+
QgsNetworkAccessManager::instance()->setCacheDisabled( checked );
230+
} );
231+
232+
mToolbar->addWidget( disableCacheCheck );
220233
}

‎src/core/network/qgsnetworkaccessmanager.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ QgsNetworkAccessManager *QgsNetworkAccessManager::instance( Qt::ConnectionType c
127127
sMainNAM = nam;
128128

129129
if ( !nam->mInitialized )
130+
{
130131
nam->setupDefaultProxyAndCache( connectionType );
132+
nam->setCacheDisabled( sMainNAM->cacheDisabled() );
133+
}
131134

132135
return nam;
133136
}
@@ -249,6 +252,13 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
249252
}
250253
#endif
251254

255+
if ( sMainNAM->mCacheDisabled )
256+
{
257+
// if caching is disabled then we override whatever the request actually has set!
258+
pReq->setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
259+
pReq->setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
260+
}
261+
252262
static QAtomicInt sRequestId = 0;
253263
const int requestId = ++sRequestId;
254264
QByteArray content;

‎src/core/network/qgsnetworkaccessmanager.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,30 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
409409
*/
410410
void setupDefaultProxyAndCache( Qt::ConnectionType connectionType = Qt::BlockingQueuedConnection );
411411

412+
#ifndef SIP_RUN
413+
414+
/**
415+
* Returns TRUE if all network caching is disabled.
416+
*
417+
* \see setCacheDisabled()
418+
* \note Not available in Python bindings.
419+
* \since QGIS 3.18
420+
*/
421+
bool cacheDisabled() const { return mCacheDisabled; }
422+
423+
/**
424+
* Sets whether all network caching should be disabled.
425+
*
426+
* If \a disabled is TRUE then all caching will be disabled, causing all requests
427+
* to be retrieved from the network regardless of the request's attributes.
428+
*
429+
* \see cacheDisabled()
430+
* \note Not available in Python bindings.
431+
* \since QGIS 3.18
432+
*/
433+
void setCacheDisabled( bool disabled ) { mCacheDisabled = disabled; }
434+
#endif
435+
412436
/**
413437
* Returns whether the system proxy should be used.
414438
*/
@@ -655,6 +679,7 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
655679
QStringList mNoProxyURLs;
656680
bool mUseSystemProxy = false;
657681
bool mInitialized = false;
682+
bool mCacheDisabled = false;
658683
static QgsNetworkAccessManager *sMainNAM;
659684
// ssl error handler, will be set for main thread ONLY
660685
std::unique_ptr< QgsSslErrorHandler > mSslErrorHandler;

0 commit comments

Comments
 (0)
Please sign in to comment.