QNetworkDiskCache Class ReferenceThe QNetworkDiskCache class provides a very basic disk cache. More... #include <QNetworkDiskCache> Inherits: QAbstractNetworkCache. This class was introduced in Qt 4.5. Public Functions
Reimplemented Public Functions
Public Slots
Protected Functions
Additional Inherited Members
Detailed DescriptionThe 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
|