Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Sep 3, 2024
1 parent f8450ad commit 98c5c49
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/app/options/qgsoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
mCacheSize->setMinimum( 0 );
mCacheSize->setMaximum( std::numeric_limits<int>::max() );
mCacheSize->setSingleStep( 50 );
qint64 cacheSize = mSettings->value( QStringLiteral( "cache/size2" ), 0 ).toLongLong();
qint64 cacheSize = mSettings->value( QStringLiteral( "cache/size_bytes" ), 0 ).toLongLong();
mCacheSize->setValue( static_cast<int>( cacheSize / 1024 / 1024 ) );
mCacheSize->setClearValue( 0 );
connect( mBrowseCacheDirectory, &QAbstractButton::clicked, this, &QgsOptions::browseCacheDirectory );
Expand Down Expand Up @@ -1583,7 +1583,7 @@ void QgsOptions::saveOptions()
else
mSettings->remove( QStringLiteral( "cache/directory" ) );

mSettings->setValue( QStringLiteral( "cache/size2" ), QVariant::fromValue( mCacheSize->value() * 1024LL * 1024LL ) );
mSettings->setValue( QStringLiteral( "cache/size_bytes" ), QVariant::fromValue( mCacheSize->value() * 1024LL * 1024LL ) );

//url with no proxy at all
QStringList noProxyUrls;
Expand Down
8 changes: 2 additions & 6 deletions src/core/network/qgsnetworkaccessmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,13 +753,9 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
if ( cacheDirectory.isEmpty() )
cacheDirectory = QStandardPaths::writableLocation( QStandardPaths::CacheLocation );
newcache->setCacheDirectory( cacheDirectory );
qint64 cacheSize = settings.value( QStringLiteral( "cache/size2" ), 0 ).toLongLong();
if ( cacheSize == 0 )
{
// Calculatre maximum cache size based on available free space
cacheSize = QgsNetworkDiskCache::smartCacheSize( cacheDirectory );
}
qint64 cacheSize = settings.value( QStringLiteral( "cache/size_bytes" ), 0 ).toLongLong();
newcache->setMaximumCacheSize( cacheSize );

QgsDebugMsgLevel( QStringLiteral( "cacheDirectory: %1" ).arg( newcache->cacheDirectory() ), 4 );
QgsDebugMsgLevel( QStringLiteral( "maximumCacheSize: %1" ).arg( newcache->maximumCacheSize() ), 4 );

Expand Down
24 changes: 16 additions & 8 deletions src/core/network/qgsnetworkdiskcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ qint64 QgsNetworkDiskCache::maximumCacheSize() const
void QgsNetworkDiskCache::setMaximumCacheSize( qint64 size )
{
const QMutexLocker lock( &sDiskCacheMutex );

if ( size == 0 )
{
// Calculate maximum cache size based on available free space
size = smartCacheSize( sDiskCache.cacheDirectory() );
}

sDiskCache.setMaximumCacheSize( size );
}

Expand Down Expand Up @@ -134,7 +141,7 @@ qint64 QgsNetworkDiskCache::smartCacheSize( const QString &cacheDir )
size += fi.size();
}

const QStringList childDirPaths = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden );
const QStringList childDirPaths = dir.entryList( QDir::Dirs | QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::NoSymLinks );
for ( const QString &childDirPath : childDirPaths )
{
size += dirSize( dirPath + QDir::separator() + childDirPath );
Expand All @@ -150,37 +157,38 @@ qint64 QgsNetworkDiskCache::smartCacheSize( const QString &cacheDir )
// NOLINTBEGIN(bugprone-narrowing-conversions)
// Logic taken from Firefox's smart cache size handling
qint64 available10MB = bytesFree / 1024 / ( 1024LL * 10 );
qint64 cacheSize10MB = 0;
if ( available10MB > 2500 )
{
// Cap the cache size to 1GB
cacheSize = 100;
cacheSize10MB = 100;
}
else
{
if ( available10MB > 700 )
{
// Add 2.5% of the free space above 7GB
cacheSize += ( available10MB - 700 ) * 0.025;
cacheSize10MB += ( available10MB - 700 ) * 0.025;
available10MB = 700;
}
if ( available10MB > 50 )
{
// Add 7.5% of free sapce between 500MB to 7GB
cacheSize += ( available10MB - 50 ) * 0.075;
// Add 7.5% of free space between 500MB to 7GB
cacheSize10MB += ( available10MB - 50 ) * 0.075;
available10MB = 50;
}

#if defined( Q_OS_ANDROID )
// On Android, smaller/older devices may have very little storage

// Add 16% of free space up to 500 MB
cacheSize += std::max( 2LL, static_cast<qint64>( available10MB * 0.16 ) );
cacheSize10MB += std::max( 2LL, static_cast<qint64>( available10MB * 0.16 ) );
#else
// Add 30% of free space up to 500 MB
cacheSize += std::max( 5LL, static_cast<qint64>( available10MB * 0.30 ) );
cacheSize10MB += std::max( 5LL, static_cast<qint64>( available10MB * 0.30 ) );
#endif
}
cacheSize = cacheSize * 10 * 1024 * 1024;
cacheSize = cacheSize10MB * 10 * 1024 * 1024;
// NOLINTEND(bugprone-narrowing-conversions)
} );

Expand Down
2 changes: 1 addition & 1 deletion src/core/network/qgsnetworkdiskcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class CORE_EXPORT QgsNetworkDiskCache : public QNetworkDiskCache
QNetworkCacheMetaData fileMetaData( const QString &fileName ) const;

/**
* Returns a smart cache size based on available megabytes.
* Returns a smart cache size, in bytes, based on available free space
* \since QGIS 3.40
*/
static qint64 smartCacheSize( const QString &path );
Expand Down
7 changes: 7 additions & 0 deletions src/core/network/qgsrangerequestcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
***************************************************************************/

#include "qgsrangerequestcache.h"
#include "qgsnetworkdiskcache.h"

#include <QtDebug>
#include <QFile>
Expand Down Expand Up @@ -75,6 +76,12 @@ bool QgsRangeRequestCache::setCacheDirectory( const QString &path )

void QgsRangeRequestCache::setCacheSize( qint64 cacheSize )
{
if ( cacheSize == 0 )
{
// Calculate maximum cache size based on available free space
cacheSize = QgsNetworkDiskCache::smartCacheSize( mCacheDir );
}

mMaxDataSize = cacheSize;
expire();
}
Expand Down
8 changes: 1 addition & 7 deletions src/core/qgstiledownloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#include "qgslogger.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsnetworkdiskcache.h"
#include "qgsrangerequestcache.h"
#include "qgssettings.h"

Expand Down Expand Up @@ -200,12 +199,7 @@ QgsTileDownloadManager::QgsTileDownloadManager()
}
cacheDirectory += QLatin1String( "http-ranges" );
mRangesCache->setCacheDirectory( cacheDirectory );
qint64 cacheSize = settings.value( QStringLiteral( "cache/size2" ), 0 ).toLongLong();
if ( cacheSize == 0 )
{
// Calculatre maximum cache size based on available free space
cacheSize = QgsNetworkDiskCache::smartCacheSize( cacheDirectory );
}
qint64 cacheSize = settings.value( QStringLiteral( "cache/size_bytes" ), 0 ).toLongLong();
mRangesCache->setCacheSize( cacheSize );
}

Expand Down
5 changes: 0 additions & 5 deletions src/server/qgsserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ void QgsServer::setupNetworkAccessManager()
const QString cacheDirectory = sSettings()->cacheDirectory();
cache->setCacheDirectory( cacheDirectory );
qint64 cacheSize = sSettings()->cacheSize();
if ( cacheSize == 0 )
{
// Calculatre maximum cache size based on available free space
cacheSize = QgsNetworkDiskCache::smartCacheSize( cacheDirectory );
}
cache->setMaximumCacheSize( cacheSize );
QgsMessageLog::logMessage( QStringLiteral( "cacheDirectory: %1" ).arg( cache->cacheDirectory() ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
QgsMessageLog::logMessage( QStringLiteral( "maximumCacheSize: %1" ).arg( cache->maximumCacheSize() ), QStringLiteral( "Server" ), Qgis::MessageLevel::Info );
Expand Down
4 changes: 2 additions & 2 deletions src/server/qgsserversettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ void QgsServerSettings::initSettings()
// cache size
const Setting sCacheSize = { QgsServerSettingsEnv::QGIS_SERVER_CACHE_SIZE,
QgsServerSettingsEnv::DEFAULT_VALUE,
QStringLiteral( "Specify the cache size" ),
QStringLiteral( "/cache/size2" ),
QStringLiteral( "Specify the cache size (0 = automatic size)" ),
QStringLiteral( "/cache/size_bytes" ),
QMetaType::Type::LongLong,
0,
QVariant()
Expand Down
3 changes: 3 additions & 0 deletions src/ui/qgsoptionsbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -4878,6 +4878,9 @@ The bigger the number, the faster zooming with the mouse wheel will be.</string>
</item>
<item row="4" column="1">
<widget class="QgsSpinBox" name="mCacheSize">
<property name="toolTip">
<string>Specify the cache size in megabytes. Clear the value to enable smart cache size, which sets the maximum cache size based on available space.</string>
</property>
<property name="suffix">
<string> MB</string>
</property>
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/qgis_server_settings/conf0.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[cache]
directory=
size2=@Variant(\0\0\0\x81\0\0\0\0\x3 \0\0)
size_bytes=@Variant(\0\0\0\x81\0\0\0\0\x3 \0\0)

[qgis]
parallel_rendering=true
Expand Down
2 changes: 1 addition & 1 deletion tests/testdata/qgis_server_settings/conf1.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[cache]
directory=/tmp/mycache
size2=@Variant(\0\0\0\x81\0\0\0\0\x3 \0\0)
size_bytes=@Variant(\0\0\0\x81\0\0\0\0\x3 \0\0)

[qgis]
parallel_rendering=false
Expand Down

0 comments on commit 98c5c49

Please sign in to comment.