Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QNetworkDiskCache Class

TheQNetworkDiskCache class provides a very basic disk cache.More...

Header:#include <QNetworkDiskCache>
Since: Qt 4.5
Inherits:QAbstractNetworkCache

Public Functions

QNetworkDiskCache(QObject * parent = 0)
~QNetworkDiskCache()
QStringcacheDirectory() const
QNetworkCacheMetaDatafileMetaData(const QString & fileName) const
qint64maximumCacheSize() const
voidsetCacheDirectory(const QString & cacheDir)
voidsetMaximumCacheSize(qint64 size)

Reimplemented Public Functions

virtual qint64cacheSize() const
virtual QIODevice *data(const QUrl & url)
virtual voidinsert(QIODevice * device)
virtual QNetworkCacheMetaDatametaData(const QUrl & url)
virtual QIODevice *prepare(const QNetworkCacheMetaData & metaData)
virtual boolremove(const QUrl & url)
virtual voidupdateMetaData(const QNetworkCacheMetaData & metaData)

Public Slots

virtual voidclear()

Protected Functions

virtual qint64expire()
  • 8 protected functions inherited fromQObject

Additional Inherited Members

  • 1 property inherited fromQObject
  • 1 signal inherited fromQObject
  • 7 static public members inherited fromQObject

Detailed Description

TheQNetworkDiskCache class provides a very basic disk cache.

QNetworkDiskCache stores each url in its own file inside of thecacheDirectory usingQDataStream. Files with a text MimeType are compressed usingqCompress. Each cache file starts with "cache_" and ends in ".cache". Data is written to disk only ininsert() andupdateMetaData().

Currently you can not share the same cache files with more then 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=newQNetworkAccessManager(this);QNetworkDiskCache*diskCache=newQNetworkDiskCache(this);diskCache->setCacheDirectory("cacheDir");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:

// do a normal request (preferred from network, as this is the default)QNetworkRequest request(QUrl(QString("http://qt.nokia.com")));manager->get(request);// do a request preferred from cacheQNetworkRequest request2(QUrl(QString("http://qt.nokia.com")));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();}

Member Function Documentation

QNetworkDiskCache::QNetworkDiskCache(QObject * parent = 0)

Creates a new disk cache. Theparent argument is passed toQAbstractNetworkCache's constructor.

QNetworkDiskCache::~QNetworkDiskCache()

Destroys the cache object. This does not clear the disk cache.

QString QNetworkDiskCache::cacheDirectory() const

Returns the location where cached files will be stored.

See alsosetCacheDirectory().

[virtual]qint64 QNetworkDiskCache::cacheSize() const

Reimplemented fromQAbstractNetworkCache::cacheSize().

[virtual slot]void QNetworkDiskCache::clear()

Reimplemented fromQAbstractNetworkCache::clear().

[virtual]QIODevice * QNetworkDiskCache::data(constQUrl & url)

Reimplemented fromQAbstractNetworkCache::data().

[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 themaximumCacheSize() older cache files are removed until the total size is less then 90% ofmaximumCacheSize() 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 thatQNetworkDiskCache does not, for example the number of times a cache is accessed.

Note:cacheSize() calls expire if the current cache size is unknown.

See alsomaximumCacheSize() andfileMetaData().

QNetworkCacheMetaData QNetworkDiskCache::fileMetaData(constQString & fileName) const

Returns theQNetworkCacheMetaData for the cache filefileName.

IffileName is not a cache fileQNetworkCacheMetaData will be invalid.

[virtual]void QNetworkDiskCache::insert(QIODevice * device)

Reimplemented fromQAbstractNetworkCache::insert().

qint64 QNetworkDiskCache::maximumCacheSize() const

Returns the current maximum size in bytes for the disk cache.

See alsosetMaximumCacheSize().

[virtual]QNetworkCacheMetaData QNetworkDiskCache::metaData(constQUrl & url)

Reimplemented fromQAbstractNetworkCache::metaData().

[virtual]QIODevice * QNetworkDiskCache::prepare(constQNetworkCacheMetaData & metaData)

Reimplemented fromQAbstractNetworkCache::prepare().

[virtual]bool QNetworkDiskCache::remove(constQUrl & url)

Reimplemented fromQAbstractNetworkCache::remove().

void QNetworkDiskCache::setCacheDirectory(constQString & cacheDir)

Sets the directory where cached files will be stored tocacheDir

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.

See alsocacheDirectory() andQDesktopServices::CacheLocation.

void QNetworkDiskCache::setMaximumCacheSize(qint64 size)

Sets the maximum size of the disk cache to besize in bytes.

If the new size is smaller then the current cache size then the cache will callexpire().

See alsomaximumCacheSize().

[virtual]void QNetworkDiskCache::updateMetaData(constQNetworkCacheMetaData & metaData)

Reimplemented fromQAbstractNetworkCache::updateMetaData().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp