1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html><head><title>QNetworkDiskCache Class Reference</title><style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
td.postheader { font-family: sans-serif }
tr.address { font-family: sans-serif }
body { background: #ffffff; color: black; }
</style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr /><td align="left" valign="top" width="32"><img align="left" border="0" height="32" src="images/rb-logo.png" width="32" /></td><td width="1">  </td><td class="postheader" valign="center"><a href="../pyqt4ref.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a></td></table><h1 align="center">QNetworkDiskCache Class Reference<br /><sup><sup>[<a href="qtnetwork.html">QtNetwork</a> module]</sup></sup></h1><p>The QNetworkDiskCache class provides a very basic disk cache. <a href="#details">More...</a></p>
<p>Inherits <a href="qabstractnetworkcache.html">QAbstractNetworkCache</a>.</p><h3>Methods</h3><ul><li><div class="fn" /><b><a href="qnetworkdiskcache.html#QNetworkDiskCache">__init__</a></b> (<i>self</i>, QObject <i>parent</i> = None)</li><li><div class="fn" />QString <b><a href="qnetworkdiskcache.html#cacheDirectory">cacheDirectory</a></b> (<i>self</i>)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#cacheSize">cacheSize</a></b> (<i>self</i>)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#clear">clear</a></b> (<i>self</i>)</li><li><div class="fn" />QIODevice <b><a href="qnetworkdiskcache.html#data">data</a></b> (<i>self</i>, QUrl)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#expire">expire</a></b> (<i>self</i>)</li><li><div class="fn" />QNetworkCacheMetaData <b><a href="qnetworkdiskcache.html#fileMetaData">fileMetaData</a></b> (<i>self</i>, QString)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#insert">insert</a></b> (<i>self</i>, QIODevice)</li><li><div class="fn" />int <b><a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a></b> (<i>self</i>)</li><li><div class="fn" />QNetworkCacheMetaData <b><a href="qnetworkdiskcache.html#metaData">metaData</a></b> (<i>self</i>, QUrl)</li><li><div class="fn" />QIODevice <b><a href="qnetworkdiskcache.html#prepare">prepare</a></b> (<i>self</i>, QNetworkCacheMetaData)</li><li><div class="fn" />bool <b><a href="qnetworkdiskcache.html#remove">remove</a></b> (<i>self</i>, QUrl)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#setCacheDirectory">setCacheDirectory</a></b> (<i>self</i>, QString)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#setMaximumCacheSize">setMaximumCacheSize</a></b> (<i>self</i>, int)</li><li><div class="fn" /><b><a href="qnetworkdiskcache.html#updateMetaData">updateMetaData</a></b> (<i>self</i>, QNetworkCacheMetaData)</li></ul><a name="details" /><hr /><h2>Detailed Description</h2><p>The QNetworkDiskCache class provides a very basic disk cache.</p>
<p>QNetworkDiskCache stores each url in its own file inside of the cacheDirectory using <a href="qdatastream.html">QDataStream</a>. Files with a text MimeType are compressed using qCompress. Each cache file starts with "<a href="qmake-environment-reference.html#cache">cache_</a>" and ends in ".cache". Data is written to disk only in <a href="qnetworkdiskcache.html#insert">insert</a>() and <a href="qnetworkdiskcache.html#updateMetaData">updateMetaData</a>().</p>
<p>Currently you can not share the same cache files with more then one disk cache.</p>
<p>QNetworkDiskCache by default limits the amount of space that the cache will use on the system to 50MB.</p>
<p>Note you have to set the cache directory before it will work.</p>
<p>A network disk cache can be enabled by:</p>
<pre> QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
diskCache->setCacheDirectory("cacheDir");
manager->setCache(diskCache);</pre>
<p>When sending requests, to control the preference of when to use the cache and when to use the network, consider the following:</p>
<pre><span class="comment"> // do a normal request (preferred from network, as this is the default)</span>
QNetworkRequest request(QUrl(QString("http:<span class="comment">//qt.nokia.com")));</span>
manager->get(request);
<span class="comment"> // do a request preferred from cache</span>
QNetworkRequest request2(QUrl(QString("http:<span class="comment">//qt.nokia.com")));</span>
request2.setAttribute(QNetworkRequest.CacheLoadControlAttribute, QNetworkRequest.PreferCache);
manager->get(request2);</pre>
<p>To check whether the response came from the cache or from the network, the following can be applied:</p>
<pre> void replyFinished(QNetworkReply *reply) {
QVariant fromCache = reply->attribute(QNetworkRequest.SourceIsFromCacheAttribute);
qDebug() << "page from cache?" << fromCache.toBool();
}</pre>
<hr /><h2>Method Documentation</h2><h3 class="fn"><a name="QNetworkDiskCache" />QNetworkDiskCache.__init__ (<i>self</i>, <a href="qobject.html">QObject</a> <i>parent</i> = None)</h3><p>The <i>parent</i> argument, if not None, causes <i>self</i> to be owned by Qt instead of PyQt.</p><p>Creates a new disk cache. The <i>parent</i> argument is passed to <a href="qabstractnetworkcache.html">QAbstractNetworkCache</a>'s constructor.</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/~QNetworkDiskCache" />
<h3 class="fn"><a name="cacheDirectory" />QString QNetworkDiskCache.cacheDirectory (<i>self</i>)</h3><p>Returns the location where cached files will be stored.</p>
<p>See also <a href="qnetworkdiskcache.html#setCacheDirectory">setCacheDirectory</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/cacheSize" />
<h3 class="fn"><a name="cacheSize" />int QNetworkDiskCache.cacheSize (<i>self</i>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#cacheSize">QAbstractNetworkCache.cacheSize</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/clear" />
<h3 class="fn"><a name="clear" />QNetworkDiskCache.clear (<i>self</i>)</h3><p>This method is also a Qt slot with the C++ signature <tt>void clear()</tt>.</p><p>Reimplemented from <a href="qabstractnetworkcache.html#clear">QAbstractNetworkCache.clear</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/data" />
<h3 class="fn"><a name="data" /><a href="qiodevice.html">QIODevice</a> QNetworkDiskCache.data (<i>self</i>, <a href="qurl.html">QUrl</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#data">QAbstractNetworkCache.data</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/expire" />
<h3 class="fn"><a name="expire" />int QNetworkDiskCache.expire (<i>self</i>)</h3><p>Cleans the cache so that its size is under the maximum cache size. Returns the current size of the cache.</p>
<p>When the current size of the cache is greater than the <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>() older cache files are removed until the total size is less then 90% of <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>() starting with the oldest ones first using the file creation date to determine how old a cache file is.</p>
<p>Subclasses can reimplement this function to change the order that cache files are removed taking into account information in the application knows about that <a href="qnetworkdiskcache.html">QNetworkDiskCache</a> does not, for example the number of times a cache is accessed.</p>
<p>Note: <a href="qnetworkdiskcache.html#cacheSize">cacheSize</a>() calls expire if the current cache size is unknown.</p>
<p>See also <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>() and <a href="qnetworkdiskcache.html#fileMetaData">fileMetaData</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/fileMetaData" />
<h3 class="fn"><a name="fileMetaData" /><a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> QNetworkDiskCache.fileMetaData (<i>self</i>, QString)</h3><p>Returns the <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> for the cache file <i>fileName</i>.</p>
<p>If <i>fileName</i> is not a cache file <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> will be invalid.</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/insert" />
<h3 class="fn"><a name="insert" />QNetworkDiskCache.insert (<i>self</i>, <a href="qiodevice.html">QIODevice</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#insert">QAbstractNetworkCache.insert</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/maximumCacheSize" />
<h3 class="fn"><a name="maximumCacheSize" />int QNetworkDiskCache.maximumCacheSize (<i>self</i>)</h3><p>Returns the current maximum size for the disk cache.</p>
<p>See also <a href="qnetworkdiskcache.html#setMaximumCacheSize">setMaximumCacheSize</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/metaData" />
<h3 class="fn"><a name="metaData" /><a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a> QNetworkDiskCache.metaData (<i>self</i>, <a href="qurl.html">QUrl</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#metaData">QAbstractNetworkCache.metaData</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/prepare" />
<h3 class="fn"><a name="prepare" /><a href="qiodevice.html">QIODevice</a> QNetworkDiskCache.prepare (<i>self</i>, <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#prepare">QAbstractNetworkCache.prepare</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/remove" />
<h3 class="fn"><a name="remove" />bool QNetworkDiskCache.remove (<i>self</i>, <a href="qurl.html">QUrl</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#remove">QAbstractNetworkCache.remove</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/setCacheDirectory" />
<h3 class="fn"><a name="setCacheDirectory" />QNetworkDiskCache.setCacheDirectory (<i>self</i>, QString)</h3><p>Sets the directory where cached files will be stored to <i>cacheDir</i></p>
<p><a href="qnetworkdiskcache.html">QNetworkDiskCache</a> will create this directory if it does not exists.</p>
<p>Prepared cache items will be stored in the new cache directory when they are inserted.</p>
<p>See also <a href="qnetworkdiskcache.html#cacheDirectory">cacheDirectory</a>() and <a href="qdesktopservices.html#StandardLocation-enum">QDesktopServices.CacheLocation</a>.</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/setMaximumCacheSize" />
<h3 class="fn"><a name="setMaximumCacheSize" />QNetworkDiskCache.setMaximumCacheSize (<i>self</i>, int)</h3><p>Sets the maximum size of the disk cache to be <i>size</i>.</p>
<p>If the new size is smaller then the current cache size then the cache will call <a href="qnetworkdiskcache.html#expire">expire</a>().</p>
<p>See also <a href="qnetworkdiskcache.html#maximumCacheSize">maximumCacheSize</a>().</p>
<a name="//apple_ref/cpp/instm/QNetworkDiskCache/updateMetaData" />
<h3 class="fn"><a name="updateMetaData" />QNetworkDiskCache.updateMetaData (<i>self</i>, <a href="qnetworkcachemetadata.html">QNetworkCacheMetaData</a>)</h3><p>Reimplemented from <a href="qabstractnetworkcache.html#updateMetaData">QAbstractNetworkCache.updateMetaData</a>().</p>
<p /><address><hr /><div align="center"><table border="0" cellspacing="0" width="100%"><tr class="address"><td align="left" width="25%">PyQt 4.7.3 for X11</td><td align="center" width="50%">Copyright © <a href="http://www.riverbankcomputing.com">Riverbank Computing Ltd</a> and <a href="http://www.qtsoftware.com">Nokia</a> 2010</td><td align="right" width="25%">Qt 4.6.2</td></tr></table></div></address></body></html>
|