QNetworkDiskCache¶
The
QNetworkDiskCache
class provides a very basic disk cache. More…
Synopsis¶
Functions¶
def
cacheDirectory
()def
fileMetaData
(fileName)def
maximumCacheSize
()def
setCacheDirectory
(cacheDir)def
setMaximumCacheSize
(size)
Virtual functions¶
def
expire
()
Detailed Description¶
QNetworkDiskCache
stores each url in its own file inside of thecacheDirectory
usingQDataStream
. Files with a text MimeType are compressed usingqCompress
. Data is written to disk only ininsert()
andupdateMetaData()
.Currently you cannot share the same cache files with more than 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:
manager = QNetworkAccessManager(self) diskCache = QNetworkDiskCache(self) 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) request = QNetworkRequest(QUrl("http://qt-project.org")) manager.get(request) # do a request preferred from cache request2 = QNetworkRequest(QUrl("http://qt-project.org")) 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:
@Slot(QNetworkReply) def replyFinished(reply): fromCache = reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute) print("page from cache? %d" % fromCache)
- class PySide2.QtNetwork.QNetworkDiskCache([parent=None])¶
- param parent:
Creates a new disk cache. The
parent
argument is passed toQAbstractNetworkCache
‘s constructor.
- PySide2.QtNetwork.QNetworkDiskCache.cacheDirectory()¶
- Return type:
str
Returns the location where cached files will be stored.
See also
- PySide2.QtNetwork.QNetworkDiskCache.expire()¶
- Return type:
int
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% ofmaximumCacheSize()
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
- PySide2.QtNetwork.QNetworkDiskCache.fileMetaData(fileName)¶
- Parameters:
fileName – str
- Return type:
Returns the
QNetworkCacheMetaData
for the cache filefileName
.If
fileName
is not a cache fileQNetworkCacheMetaData
will be invalid.
- PySide2.QtNetwork.QNetworkDiskCache.maximumCacheSize()¶
- Return type:
int
Returns the current maximum size for the disk cache.
See also
- PySide2.QtNetwork.QNetworkDiskCache.setCacheDirectory(cacheDir)¶
- Parameters:
cacheDir – str
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()
CacheLocation
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.