Skip to content

PythonQt.QtNetwork.QNetworkDiskCache

QNetworkDiskCache

PythonQtInstanceWrapper object

Native bases: PythonQtInstanceWrapper

Supplemental documentation: Qt 5 reference:

Detailed Description

QNetworkDiskCache stores each url in its own file inside of the cacheDirectory using QDataStream. Files with a text MimeType are compressed using qCompress. Data is written to disk only in insert() and updateMetaData().

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:

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-project.org")));
manager->get(request);

// do a request preferred from cache
QNetworkRequest request2(QUrl(QString("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:

void replyFinished(QNetworkReply *reply) {
    QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
    qDebug() << "page from cache?" << fromCache.toBool();
}

Source: https://doc.qt.io/archives/qt-5.15/qnetworkdiskcache.html

Complete member index

Every captured member retains its own identity. Identical captured declaration text is linked once; this does not establish native inheritance or interchangeable objects.

Member Kind Signature Summary
__init__ function (self, *args: Any, **kwargs: Any) -> None Native constructor; consult class documentation for supported overloads. (shared captured text)
blockSignals function (self, b: Any) -> bool X.blockSignals(b) -> bool (shared captured text)
cacheDirectory function (self) -> str X.cacheDirectory() -> str
cacheSize function (self) -> int X.cacheSize() -> int
childEvent function (self, event: Any) -> Any (shared captured text)
children function (self) -> tuple X.children() -> tuple (shared captured text)
className function (self, *args: Any, **kwargs: Any) -> Any Return the classname of the object (shared captured text)
clear function (self) -> None Reimplements: [QAbstractNetworkCache::clear](https://doc.qt.io/archives/qt-5.15/qabstractnetworkcache.html#clear)().
connect function (self, signal: Any, receiver: Any, slot: Any, type: Any) -> bool X.connect(signal, receiver, slot, type) -> bool (shared captured text)
customEvent function (self, event: Any) -> Any (shared captured text)
data function (self, url: Any) -> Any X.data(url) -> PythonQt.QtCore.QIODevice
delete function (self, *args: Any, **kwargs: Any) -> Any Deletes the C++ object (at your own risk, my friend!) (shared captured text)
deleteLater function (self) -> Any (shared captured text)
destroyed function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QNetworkDiskCache.destroyed; argument details are not documented.
disconnect function (self, signal: Any, receiver: Any, slot: Any) -> bool X.disconnect(signal, receiver, slot) -> bool (shared captured text)
dumpObjectInfo function (self) -> Any (shared captured text)
dumpObjectTree function (self) -> Any (shared captured text)
dynamicPropertyNames function (self) -> Any (shared captured text)
event function (self, event: Any) -> bool X.event(event) -> bool (shared captured text)
eventFilter function (self, watched: Any, event: Any) -> bool X.eventFilter(watched, event) -> bool (shared captured text)
expire function (self) -> int X.expire() -> int
fileMetaData function (self, fileName: str) -> Any X.fileMetaData(fileName) -> PythonQt.QtNetwork.QNetworkCacheMetaData
findChild function (self, type: Any, name: Any) -> Any X.findChild(type, name) -> PythonQt.private.QObject (shared captured text)
findChildren function (self, type: Any, name: Any) -> tuple X.findChildren(type, name) -> tuple (shared captured text)
help function (self, *args: Any, **kwargs: Any) -> Any Shows the help of available methods for this class (shared captured text)
inherits function (self, *args: Any, **kwargs: Any) -> Any Returns if the class inherits or is of given type name (shared captured text)
insert function (self, device: Any) -> None Reimplements: [QAbstractNetworkCache::insert](https://doc.qt.io/archives/qt-5.15/qabstractnetworkcache.html#insert)(QIODevice *device).
installEventFilter function (self, filterObj: Any) -> Any (shared captured text)
isSignalConnected function (self, signal: Any) -> bool X.isSignalConnected(signal) -> bool (shared captured text)
isWidgetType function (self) -> bool X.isWidgetType() -> bool (shared captured text)
isWindowType function (self) -> bool X.isWindowType() -> bool (shared captured text)
killTimer function (self, id: Any) -> Any (shared captured text)
maximumCacheSize function (self) -> int X.maximumCacheSize() -> int
metaData function (self, url: Any) -> Any X.metaData(url) -> PythonQt.QtNetwork.QNetworkCacheMetaData
metaObject function (self) -> Any X.metaObject() -> PythonQt.QtCore.QMetaObject (shared captured text)
moveToThread function (self, thread: Any) -> Any (shared captured text)
objectName attribute Any (shared captured text)
objectNameChanged function (self, *args: Any, **kwargs: Any) -> Any Native PythonQt.QtNetwork.QNetworkDiskCache.objectNameChanged; argument details are not documented.
parent function (self) -> Any X.parent() -> PythonQt.private.QObject (shared captured text)
prepare function (self, metaData: Any) -> Any X.prepare(metaData) -> PythonQt.QtCore.QIODevice
property function (self, name: Any) -> Any X.property(name) -> object (shared captured text)
remove function (self, url: Any) -> bool X.remove(url) -> bool
removeEventFilter function (self, obj: Any) -> Any (shared captured text)
sender function (self) -> Any X.sender() -> PythonQt.private.QObject (shared captured text)
senderSignalIndex function (self) -> int X.senderSignalIndex() -> int (shared captured text)
setCacheDirectory function (self, cacheDir: str) -> None Sets the directory where cached files will be stored to cacheDir
setMaximumCacheSize function (self, size: int) -> None Sets the maximum size of the disk cache to be size.
setObjectName function (self, name: Any) -> Any (shared captured text)
setParent function (self, parent: Any) -> Any (shared captured text)
setProperty function (self, name: Any, value: Any) -> bool X.setProperty(name, value) -> bool (shared captured text)
signalsBlocked function (self) -> bool X.signalsBlocked() -> bool (shared captured text)
startTimer function (self, interval: Any, timerType: Any) -> int X.startTimer(interval, timerType) -> int (shared captured text)
thread function (self) -> Any (shared captured text)
timerEvent function (self, event: Any) -> Any (shared captured text)
tr function (self, text: Any, ambig: Any, n: Any) -> str X.tr(text, ambig, n) -> str (shared captured text)
updateMetaData function (self, metaData: Any) -> None Reimplements: [QAbstractNetworkCache::updateMetaData](https://doc.qt.io/archives/qt-5.15/qabstractnetworkcache.html#updateMetaData)(const QNetworkCacheMetaData \&metaData).