QNetworkDiskCache Class Reference

The QNetworkDiskCache class provides a very basic disk cache.

  1. #include <QNetworkDiskCache>

Inherits: QAbstractNetworkCache.

This class was introduced in Qt 4.5.

Detailed Description

The QNetworkDiskCache class provides a very basic disk cache.

QNetworkDiskCache stores each url in its own file inside of the cacheDirectory using QDataStream. Files with a text MimeType are compressed using qCompress. Each cache file starts with "cache_" and ends in ".cache". Data is written to disk only in insert() and updateMetaData().

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:

  1.                 QNetworkAccessManager *manager = new QNetworkAccessManager(this);
  2. QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
  3. diskCache->setCacheDirectory("cacheDir");
  4. 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:

  1. // do a normal request (preferred from network, as this is the default)
  2. QNetworkRequest request(QUrl(QString("http://qt.nokia.com")));
  3. manager->get(request);
  4.  
  5. // do a request preferred from cache
  6. QNetworkRequest request2(QUrl(QString("http://qt.nokia.com")));
  7. request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
  8. manager->get(request2);

To check whether the response came from the cache or from the network, the following can be applied:

  1. void replyFinished(QNetworkReply *reply)  {
  2.     QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
  3.     qDebug() << "page from cache?" << fromCache.toBool();
  4. }

Public Functions

Toggle details QNetworkDiskCache

QNetworkDiskCacheQNetworkDiskCache ( QObject *parent=0 ) ( QObject *parent=0 )

Creates a new disk cache. The parent argument is passed to QAbstractNetworkCache's constructor.

Look up this member in the source code.

Toggle details QNetworkDiskCache

~QNetworkDiskCache~QNetworkDiskCache () ()

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

Look up this member in the source code.

Toggle details QString QNetworkDiskCache

cacheDirectorycacheDirectory () ()const

Returns the location where cached files will be stored.

See also setCacheDirectory().

Look up this member in the source code.

Toggle details qint64 QNetworkDiskCache

cacheSizecacheSize () ()const [virtual]

Reimplemented from QAbstractNetworkCache::cacheSize().

Look up this member in the source code.

Toggle details QIODevice * QNetworkDiskCache

datadata ( const QUrl &url ) ( const QUrl &url ) [virtual]

Reimplemented from QAbstractNetworkCache::data().

Look up this member in the source code.

Toggle details qint64 QNetworkDiskCache

expireexpire () () [virtual protected]

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.

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

See also maximumCacheSize() and fileMetaData().

Look up this member in the source code.

Toggle details QNetworkCacheMetaData QNetworkDiskCache

fileMetaDatafileMetaData ( const QString &fileName ...) ( const QString &fileName )const

Returns the QNetworkCacheMetaData for the cache file fileName.

If fileName is not a cache file QNetworkCacheMetaData will be invalid.

Look up this member in the source code.

Toggle details void QNetworkDiskCache

insertinsert ( QIODevice *device ) ( QIODevice *device ) [virtual]

Reimplemented from QAbstractNetworkCache::insert().

Look up this member in the source code.

Toggle details qint64 QNetworkDiskCache

maximumCacheSizemaximumCacheSize () ()const

Returns the current maximum size for the disk cache.

See also setMaximumCacheSize().

Look up this member in the source code.

Toggle details QNetworkCacheMetaData QNetworkDiskCache

metaDatametaData ( const QUrl &url ) ( const QUrl &url ) [virtual]

Reimplemented from QAbstractNetworkCache::metaData().

Look up this member in the source code.

Toggle details QIODevice * QNetworkDiskCache

prepareprepare ( const QNetworkCacheMetaData &metaData ...) ( const QNetworkCacheMetaData &metaData ) [virtual]

Reimplemented from QAbstractNetworkCache::prepare().

Look up this member in the source code.

Toggle details bool QNetworkDiskCache

removeremove ( const QUrl &url ) ( const QUrl &url ) [virtual]

Reimplemented from QAbstractNetworkCache::remove().

Look up this member in the source code.

Toggle details void QNetworkDiskCache

setCacheDirectorysetCacheDirectory ( const QString &cacheDir ) ( const QString &cacheDir )

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.

See also cacheDirectory() and QDesktopServices::CacheLocation.

Look up this member in the source code.

Toggle details void QNetworkDiskCache

setMaximumCacheSizesetMaximumCacheSize ( qint64 size ) ( qint64 size )

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().

See also maximumCacheSize().

Look up this member in the source code.

Toggle details void QNetworkDiskCache

updateMetaDataupdateMetaData ( const QNetworkCacheMetaData &metaData ...) ( const QNetworkCacheMetaData &metaData ) [virtual]

Look up this member in the source code.

    Public Slots

    Toggle details void QNetworkDiskCache

    clearclear () () [virtual] [slot]

    Reimplemented from QAbstractNetworkCache::clear().

    Look up this member in the source code.

      Notes provided by the Qt Community
      Informative
      • 0

      Votes: 0

      Coverage: Qt library 4.7, 4.8, 5.0

      Picture of sigrid sigrid

      Lab Rat
      20 notes

      This person works for Qt Development Frameworks. Nokia Certified Qt Developer

      Article about how HTTP caching works

      For in-depth information on how HTTP caching works in general and how it works with Qt, see this blog. [labs.qt.nokia.com]

      [Revisions]