QNetworkDiskCache 类提供非常基本的磁盘缓存。 更多...
头: | #include <QNetworkDiskCache> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network) |
qmake: | QT += network |
继承: | QAbstractNetworkCache |
QNetworkDiskCache (QObject * parent = nullptr) | |
virtual | ~QNetworkDiskCache () |
QString | cacheDirectory () const |
QNetworkCacheMetaData | fileMetaData (const QString & fileName ) const |
qint64 | maximumCacheSize () const |
void | setCacheDirectory (const QString & cacheDir ) |
void | setMaximumCacheSize (qint64 size ) |
virtual qint64 | cacheSize () const override |
virtual QIODevice * | data (const QUrl & url ) override |
virtual void | insert (QIODevice * device ) override |
virtual QNetworkCacheMetaData | metaData (const QUrl & url ) override |
virtual QIODevice * | prepare (const QNetworkCacheMetaData & metaData ) override |
virtual bool | remove (const QUrl & url ) override |
virtual void | updateMetaData (const QNetworkCacheMetaData & metaData ) override |
virtual void | clear () override |
virtual qint64 | expire () |
QNetworkDiskCache stores each url in its own file inside of the cacheDirectory 使用 QDataStream . Files with a text MimeType are compressed using qCompress . Data is written to disk only in insert () 和 updateMetaData ().
Currently you cannot share the same cache files with more than one disk cache.
QNetworkDiskCache by default limits the amount of space that the cache will use on the system to 50MB.
Note you have to set the cache directory before it will work.
A network disk cache can be enabled by:
QNetworkAccessManager *manager = new QNetworkAccessManager(this); QNetworkDiskCache *diskCache = new QNetworkDiskCache(this); QString directory = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1StringView("/cacheDir/"); diskCache->setCacheDirectory(directory); manager->setCache(diskCache);
When sending requests, to control the preference of when to use the cache and when to use the network, consider the following:
using namespace Qt::StringLiterals; // do a normal request (preferred from network, as this is the default) QNetworkRequest request(QUrl(u"http://qt-project.org"_s)); manager->get(request); // do a request preferred from cache QNetworkRequest request2(QUrl(u"http://qt-project.org"_s)); request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); manager->get(request2);
To check whether the response came from the cache or from the network, the following can be applied:
void replyFinished(QNetworkReply *reply) { QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute); qDebug() << "page from cache?" << fromCache.toBool(); }
[explicit]
QNetworkDiskCache::
QNetworkDiskCache
(
QObject
*
parent
= nullptr)
Creates a new disk cache. The parent 自变量会被传递给 QAbstractNetworkCache 的构造函数。
[virtual noexcept]
QNetworkDiskCache::
~QNetworkDiskCache
()
Destroys the cache object. This does not clear the disk cache.
Returns the location where cached files will be stored.
另请参阅 setCacheDirectory ().
[override virtual]
qint64
QNetworkDiskCache::
cacheSize
() const
重实现: QAbstractNetworkCache::cacheSize() const .
[override virtual slot]
void
QNetworkDiskCache::
clear
()
重实现: QAbstractNetworkCache::clear ().
[override virtual]
QIODevice
*QNetworkDiskCache::
data
(const
QUrl
&
url
)
重实现: QAbstractNetworkCache::data (const QUrl &url).
[virtual protected]
qint64
QNetworkDiskCache::
expire
()
Cleans the cache so that its size is under the maximum cache size. Returns the current size of the cache.
When the current size of the cache is greater than the maximumCacheSize () older cache files are removed until the total size is less then 90% of maximumCacheSize () starting with the oldest ones first using the file creation date to determine how old a cache file is.
Subclasses can reimplement this function to change the order that cache files are removed taking into account information in the application knows about that QNetworkDiskCache does not, for example the number of times a cache is accessed.
注意: cacheSize () calls expire if the current cache size is unknown.
另请参阅 maximumCacheSize () 和 fileMetaData ().
返回 QNetworkCacheMetaData for the cache file fileName .
若 fileName is not a cache file QNetworkCacheMetaData will be invalid.
[override virtual]
void
QNetworkDiskCache::
insert
(
QIODevice
*
device
)
重实现: QAbstractNetworkCache::insert (QIODevice *device).
Returns the current maximum size for the disk cache.
另请参阅 setMaximumCacheSize ().
[override virtual]
QNetworkCacheMetaData
QNetworkDiskCache::
metaData
(const
QUrl
&
url
)
重实现: QAbstractNetworkCache::metaData (const QUrl &url).
[override virtual]
QIODevice
*QNetworkDiskCache::
prepare
(const
QNetworkCacheMetaData
&
metaData
)
重实现: QAbstractNetworkCache::prepare (const QNetworkCacheMetaData &metaData).
[override virtual]
bool
QNetworkDiskCache::
remove
(const
QUrl
&
url
)
重实现: QAbstractNetworkCache::remove (const QUrl &url).
Sets the directory where cached files will be stored to cacheDir
QNetworkDiskCache will create this directory if it does not exists.
Prepared cache items will be stored in the new cache directory when they are inserted.
另请参阅 cacheDirectory () 和 QStandardPaths::CacheLocation .
Sets the maximum size of the disk cache to be size .
If the new size is smaller then the current cache size then the cache will call expire ().
另请参阅 maximumCacheSize ().
[override virtual]
void
QNetworkDiskCache::
updateMetaData
(const
QNetworkCacheMetaData
&
metaData
)
重实现: QAbstractNetworkCache::updateMetaData (const QNetworkCacheMetaData &metaData).