QNetworkDiskCache Class Reference

[ QtNetwork module]

QNetworkDiskCache 类提供非常基本的磁盘缓存。 更多...

继承 QAbstractNetworkCache .

方法


详细描述

The QNetworkDiskCache class provides a very basic disk 缓存。

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 () 和 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();
 }
			

方法文档编制

QNetworkDiskCache.__init__ ( self , QObject   parent  = None)

parent argument, if not None, causes self to be owned by Qt instead of PyQt.

Creates a new disk cache. The parent argument is passed to QAbstractNetworkCache 's 构造函数。

QString QNetworkDiskCache.cacheDirectory ( self )

Returns the location where cached files will be stored.

另请参阅 setCacheDirectory ().

int QNetworkDiskCache.cacheSize ( self )

重实现自 QAbstractNetworkCache.cacheSize ().

QNetworkDiskCache.clear ( self )

This method is also a Qt slot with the C++ signature void clear() .

重实现自 QAbstractNetworkCache.clear ().

QIODevice QNetworkDiskCache.data ( self , QUrl   url )

重实现自 QAbstractNetworkCache.data ().

int QNetworkDiskCache.expire ( self )

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

QNetworkCacheMetaData QNetworkDiskCache.fileMetaData ( self , QString  fileName )

返回 QNetworkCacheMetaData 为 cache file fileName .

fileName is not a cache file QNetworkCacheMetaData 将是 无效的。

QNetworkDiskCache.insert ( self , QIODevice   device )

重实现自 QAbstractNetworkCache.insert ().

int QNetworkDiskCache.maximumCacheSize ( self )

Returns the current maximum size in bytes for the disk 缓存。

另请参阅 setMaximumCacheSize ().

QNetworkCacheMetaData QNetworkDiskCache.metaData ( self , QUrl   url )

重实现自 QAbstractNetworkCache.metaData ().

QIODevice QNetworkDiskCache.prepare ( self , QNetworkCacheMetaData   metaData )

重实现自 QAbstractNetworkCache.prepare ().

bool QNetworkDiskCache.remove ( self , QUrl   url )

重实现自 QAbstractNetworkCache.remove ().

QNetworkDiskCache.setCacheDirectory ( self , 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.

另请参阅 cacheDirectory () 和 QDesktopServices.CacheLocation .

QNetworkDiskCache.setMaximumCacheSize ( self , int  size )

Sets the maximum size of the disk cache to be size in 字节。

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

另请参阅 maximumCacheSize ().

QNetworkDiskCache.updateMetaData ( self , QNetworkCacheMetaData   metaData )

重实现自 QAbstractNetworkCache.updateMetaData ().