QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 <QtDebug>
26
31
32QByteArray QgsRangeRequestCache::entry( const QNetworkRequest &request )
33{
34 QString hash = rangeFileName( request );
35 QByteArray arr = readFile( hash );
36 return arr;
37}
38
39bool QgsRangeRequestCache::hasEntry( const QNetworkRequest &request )
40{
41 QDir dir( mCacheDir );
42 return dir.exists( rangeFileName( request ) );
43}
44
45void QgsRangeRequestCache::registerEntry( const QNetworkRequest &request, QByteArray data )
46{
47 QString hash = rangeFileName( request );
48 writeFile( hash, data );
49 expire();
50}
51
53{
54 QDir dir( mCacheDir );
55 for ( QFileInfo info : dir.entryInfoList() )
56 {
57 removeFile( info.filePath() );
58 }
59}
60
61bool QgsRangeRequestCache::setCacheDirectory( const QString &path )
62{
63 QString cachePath = path;
64 if ( !cachePath.endsWith( QDir::separator() ) )
65 {
66 cachePath.push_back( QDir::separator() );
67 }
68 mCacheDir = cachePath;
69
70 if ( !QDir().mkpath( mCacheDir ) )
71 {
72 mError = QObject::tr( "Unable to create cache directory \"%1\"" ).arg( mCacheDir );
73 return false;
74 }
75 return true;
76}
77
78void QgsRangeRequestCache::setCacheSize( qint64 cacheSize )
79{
80 if ( cacheSize == 0 )
81 {
82 // Calculate maximum cache size based on available free space
83 cacheSize = QgsNetworkDiskCache::smartCacheSize( mCacheDir );
84 }
85
86 mMaxDataSize = cacheSize;
87 expire();
88}
89
90QString QgsRangeRequestCache::rangeFileName( const QNetworkRequest &request ) const
91{
92 return mCacheDir + QStringLiteral( "%1-%2" ).arg( qHash( request.url().toString() ) ).arg( QString::fromUtf8( request.rawHeader( "Range" ) ) );
93}
94
95QByteArray QgsRangeRequestCache::readFile( const QString &fileName )
96{
97 QFile file( fileName );
98 if ( !file.open( QFile::OpenModeFlag::ReadOnly ) )
99 {
100 mError = QObject::tr( "Unable to read cache file \"%1\"" ).arg( fileName );
101 return QByteArray();
102 }
103 return file.readAll();
104}
105
106bool QgsRangeRequestCache::writeFile( const QString &fileName, QByteArray data )
107{
108 QFile file( fileName );
109 if ( !file.open( QFile::OpenModeFlag::WriteOnly ) )
110 {
111 mError = QObject::tr( "Unable to open cache file \"%1\"" ).arg( fileName );
112 return false;
113 }
114 qint64 written = file.write( data );
115 file.close();
116 if ( written != data.size() )
117 {
118 mError = QObject::tr( "Unable to write to cache file \"%1\", error: \"%2\"" ).arg( fileName, file.errorString() );
119 return false;
120 }
121 return true;
122}
123
124bool QgsRangeRequestCache::removeFile( const QString &fileName )
125{
126 if ( fileName.isEmpty() )
127 return false;
128 bool wasRemoved = QFile::remove( fileName );
129 if ( !wasRemoved )
130 {
131 mError = QObject::tr( "Unable to remove cache file \"%1\"" ).arg( fileName );
132 }
133 return wasRemoved;
134}
135
136void QgsRangeRequestCache::expire()
137{
138 QFileInfoList filesList = cacheEntries();
139 qint64 totalSize = 0;
140 for ( QFileInfo &info : filesList )
141 {
142 totalSize += info.size();
143 }
144 while ( totalSize > mMaxDataSize )
145 {
146 QFileInfo info = filesList.back();
147 filesList.pop_back();
148 totalSize -= info.size();
149 removeFile( info.filePath() );
150 }
151}
152
153QFileInfoList QgsRangeRequestCache::cacheEntries()
154{
155 QDir dir( mCacheDir );
156 QFileInfoList filesList = dir.entryInfoList( QDir::Filter::Files, QDir::SortFlags() );
157 std::sort( filesList.begin(), filesList.end(), []( QFileInfo & f1, QFileInfo & f2 )
158 {
159 QDateTime t1 = f1.fileTime( QFile::FileTime::FileAccessTime );
160 if ( !t1.isValid() )
161 t1 = f1.fileTime( QFile::FileTime::FileBirthTime );
162 QDateTime t2 = f2.fileTime( QFile::FileTime::FileAccessTime );
163 if ( !t2.isValid() )
164 t2 = f2.fileTime( QFile::FileTime::FileBirthTime );
165 return t1 > t2;
166 } );
167 return filesList;
168}
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:603