Qt Reference Documentation

# QNetworkDiskCache Class Reference

The QNetworkDiskCache class provides a very basic disk cache. More...

     #include <QNetworkDiskCache>

Inherits: QAbstractNetworkCache.

This class was introduced in Qt 4.5.

- List of all members, including inherited members

## Public Functions

| QNetworkDiskCache ( QObject * parent = 0 )
| ~QNetworkDiskCache ()
QString | cacheDirectory () const
QNetworkCacheMetaData | fileMetaData ( const QString & fileName ) const
qint64 | maximumCacheSize () const
void | setCacheDirectory ( const QString & cacheDir )
void | setMaximumCacheSize ( qint64 size )

## Reimplemented Public Functions

virtual qint64 | cacheSize () const
virtual QIODevice * | data ( const QUrl & url )
virtual void | insert ( QIODevice * device )
virtual QNetworkCacheMetaData | metaData ( const QUrl & url )
virtual QIODevice * | prepare ( const QNetworkCacheMetaData & metaData )
virtual bool | remove ( const QUrl & url )
virtual void | updateMetaData ( const QNetworkCacheMetaData & metaData )

- 7 public functions inherited from QAbstractNetworkCache
- 29 public functions inherited from QObject

## Public Slots

virtual void | clear ()

- 1 public slot inherited from QAbstractNetworkCache
- 1 public slot inherited from QObject

## Protected Functions

virtual qint64 | expire ()

- 8 protected functions inherited from QObject

### Additional Inherited Members

- 1 property inherited from QObject
- 1 signal inherited from QObject
- 7 static public members inherited from QObject

## 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:

     QNetworkAccessManager *manager = new QNetworkAccessManager(this);
     QNetworkDiskCache *diskCache = new QNetworkDiskCache(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 cache
     QNetworkRequest 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. The parent argument is passed to QAbstractNetworkCache'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 also setCacheDirectory().

### qint64 QNetworkDiskCache::cacheSize () const`[virtual]`

Reimplemented from QAbstractNetworkCache::cacheSize().

### void QNetworkDiskCache::clear ()`[virtual slot]`

Reimplemented from QAbstractNetworkCache::clear().

### QIODevice * QNetworkDiskCache::data ( const QUrl & url )`[virtual]`

Reimplemented from QAbstractNetworkCache::data().

### qint64 QNetworkDiskCache::expire ()`[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().

### QNetworkCacheMetaData QNetworkDiskCache::fileMetaData ( const QString & fileName ) const

Returns the QNetworkCacheMetaData for the cache file fileName.

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

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

Reimplemented from QAbstractNetworkCache::insert().

### qint64 QNetworkDiskCache::maximumCacheSize () const

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

See also setMaximumCacheSize().

### QNetworkCacheMetaData QNetworkDiskCache::metaData ( const QUrl & url )`[virtual]`

Reimplemented from QAbstractNetworkCache::metaData().

### QIODevice * QNetworkDiskCache::prepare ( const QNetworkCacheMetaData & metaData )`[virtual]`

Reimplemented from QAbstractNetworkCache::prepare().

### bool QNetworkDiskCache::remove ( const QUrl & url )`[virtual]`

Reimplemented from QAbstractNetworkCache::remove().

### void QNetworkDiskCache::setCacheDirectory ( 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.

### void QNetworkDiskCache::setMaximumCacheSize ( qint64 size )

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

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

See also maximumCacheSize().

### void QNetworkDiskCache::updateMetaData ( const QNetworkCacheMetaData & metaData )`[virtual]`

Reimplemented from QAbstractNetworkCache::updateMetaData().