QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsrangerequestcache.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsrangerequestcache.cpp
3 --------------------
4 begin : April 2022
5 copyright : (C) 2022 by Belgacem Nedjima
6 email : belgacem dot nedjima at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsnetworkdiskcache.h"
21
22#include <QDateTime>
23#include <QDir>
24#include <QFile>
25#include <QString>
26#include <QtDebug>
27
28using namespace Qt::StringLiterals;
29
34
35QByteArray QgsRangeRequestCache::entry( const QNetworkRequest &request )
36{
37 QString hash = rangeFileName( request );
38 QByteArray arr = readFile( hash );
39 return arr;
40}
41
42bool QgsRangeRequestCache::hasEntry( const QNetworkRequest &request )
43{
44 QDir dir( mCacheDir );
45 return dir.exists( rangeFileName( request ) );
46}
47
48void QgsRangeRequestCache::registerEntry( const QNetworkRequest &request, QByteArray data )
49{
50 QString hash = rangeFileName( request );
51 writeFile( hash, data );
52 expire();
53}
54
56{
57 QDir dir( mCacheDir );
58 for ( QFileInfo info : dir.entryInfoList() )
59 {
60 removeFile( info.filePath() );
61 }
62}
63
64bool QgsRangeRequestCache::setCacheDirectory( const QString &path )
65{
66 QString cachePath = path;
67 if ( !cachePath.endsWith( QDir::separator() ) )
68 {
69 cachePath.push_back( QDir::separator() );
70 }
71 mCacheDir = cachePath;
72
73 if ( !QDir().mkpath( mCacheDir ) )
74 {
75 mError = QObject::tr( "Unable to create cache directory \"%1\"" ).arg( mCacheDir );
76 return false;
77 }
78 return true;
79}
80
81void QgsRangeRequestCache::setCacheSize( qint64 cacheSize )
82{
83 if ( cacheSize == 0 )
84 {
85 // Calculate maximum cache size based on available free space
86 cacheSize = QgsNetworkDiskCache::smartCacheSize( mCacheDir );
87 }
88
89 mMaxDataSize = cacheSize;
90 expire();
91}
92
93QString QgsRangeRequestCache::rangeFileName( const QNetworkRequest &request ) const
94{
95 return mCacheDir + u"%1-%2"_s.arg( qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader( "Range" ) ) );
96}
97
98QByteArray QgsRangeRequestCache::readFile( const QString &fileName )
99{
100 QFile file( fileName );
101 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
102 {
103 mError = QObject::tr( "Unable to read cache file \"%1\"" ).arg( fileName );
104 return QByteArray();
105 }
106 return file.readAll();
107}
108
109bool QgsRangeRequestCache::writeFile( const QString &fileName, QByteArray data )
110{
111 QFile file( fileName );
112 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
113 {
114 mError = QObject::tr( "Unable to open cache file \"%1\"" ).arg( fileName );
115 return false;
116 }
117 qint64 written = file.write( data );
118 file.close();
119 if ( written != data.size() )
120 {
121 mError = QObject::tr( "Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
122 return false;
123 }
124 return true;
125}
126
127bool QgsRangeRequestCache::removeFile( const QString &fileName )
128{
129 if ( fileName.isEmpty() )
130 return false;
131 bool wasRemoved = QFile::remove( fileName );
132 if ( !wasRemoved )
133 {
134 mError = QObject::tr( "Unable to remove cache file \"%1\"" ).arg( fileName );
135 }
136 return wasRemoved;
137}
138
139void QgsRangeRequestCache::expire()
140{
141 QFileInfoList filesList = cacheEntries();
142 qint64 totalSize = 0;
143 for ( QFileInfo &info : filesList )
144 {
145 totalSize += info.size();
146 }
147 while ( totalSize > mMaxDataSize )
148 {
149 QFileInfo info = filesList.back();
150 filesList.pop_back();
151 totalSize -= info.size();
152 removeFile( info.filePath() );
153 }
154}
155
156QFileInfoList QgsRangeRequestCache::cacheEntries()
157{
158 QDir dir( mCacheDir );
159 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
160 std::sort( filesList.begin(), filesList.end(), []( QFileInfo & f1, QFileInfo & f2 )
161 {
162 QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
163 if ( !t1.isValid() )
164 t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
165 QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
166 if ( !t2.isValid() )
167 t2 = f2.fileTime( QFile::FileTime::FileBirthTime );
168 return t1 > t2;
169 } );
170 return filesList;
171}
static qint64 smartCacheSize(const QString &path)
Returns a smart cache size, in bytes, based on available free space.
void clear()
Clears the cache removing all of the files.
bool hasEntry(const QNetworkRequest &request)
Checks whether the range request exists in the cache.
bool setCacheDirectory(const QString &path)
Set the Cache Directory object.
QByteArray entry(const QNetworkRequest &request)
Returns the range request data stored in the cache and an empty byte array if the data is not in the ...
void registerEntry(const QNetworkRequest &request, QByteArray data)
Adds the range request data into the cache.
void setCacheSize(qint64 maxBytes)
Sets the cache size.
uint qHash(const QVariant &variant)
Hash for QVariant.
Definition qgis.cpp:611