CopperSpice API  1.7.2
QNetworkDiskCache Class Reference

The QNetworkDiskCache class provides a very basic disk cache. More...

Inheritance diagram for QNetworkDiskCache:
QAbstractNetworkCache QObject

Public Slots

void clear ()
 
- Public Slots inherited from QAbstractNetworkCache
virtual void clear ()
 
- Public Slots inherited from QObject
void deleteLater ()
 

Public Methods

 QNetworkDiskCache (QObject *parent=nullptr)
 
 ~QNetworkDiskCache ()
 
QString cacheDirectory () const
 
qint64 cacheSize () const override
 
QIODevicedata (const QUrl &url) override
 
QNetworkCacheMetaData fileMetaData (const QString &fileName) const
 
void insert (QIODevice *device) override
 
qint64 maximumCacheSize () const
 
QNetworkCacheMetaData metaData (const QUrl &url) override
 
QIODeviceprepare (const QNetworkCacheMetaData &metaData) override
 
bool remove (const QUrl &url) override
 
void setCacheDirectory (const QString &cacheDir)
 
void setMaximumCacheSize (qint64 size)
 
void updateMetaData (const QNetworkCacheMetaData &metaData) override
 
- Public Methods inherited from QAbstractNetworkCache
virtual ~QAbstractNetworkCache ()
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &objName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Protected Methods

virtual qint64 expire ()
 
- Protected Methods inherited from QAbstractNetworkCache
 QAbstractNetworkCache (QObject *parent=nullptr)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Additional Inherited Members

- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The 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:

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("https://www.copperspice.com")));
manager->get(request);
// do a request preferred from cache
QNetworkRequest request2(QUrl(QString("https://www.copperspice.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();
}
See also
setCacheDirectory()

Constructor & Destructor Documentation

QNetworkDiskCache::QNetworkDiskCache ( QObject parent = nullptr)
explicit

Creates a new disk cache. The parent argument is passed to QAbstractNetworkCache's constructor.

QNetworkDiskCache::~QNetworkDiskCache ( )

Destroys the cache object. This does not clear the disk cache.

Method Documentation

QString QNetworkDiskCache::cacheDirectory ( ) const

Returns the location where cached files will be stored.

See also
setCacheDirectory()
qint64 QNetworkDiskCache::cacheSize ( ) const
overridevirtual
void QNetworkDiskCache::clear ( )
slot

Reimplemented from QAbstractNetworkCache::clear().

QIODevice * QNetworkDiskCache::data ( const QUrl url)
overridevirtual

Reimplemented from QAbstractNetworkCache::data().

Implements QAbstractNetworkCache.

qint64 QNetworkDiskCache::expire ( )
protectedvirtual

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.

Note: cacheSize() calls expire if the current cache size is unknown.

See also
maximumCacheSize(), fileMetaData()
QNetworkCacheMetaData QNetworkDiskCache::fileMetaData ( const QString fileName) const

Returns the QNetworkCacheMetaData for the cache file fileName.

If fileName is not a cache file QNetworkCacheMetaData will be invalid.

void QNetworkDiskCache::insert ( QIODevice device)
overridevirtual

Reimplemented from QAbstractNetworkCache::insert().

Implements QAbstractNetworkCache.

qint64 QNetworkDiskCache::maximumCacheSize ( ) const

Returns the current maximum size in bytes for the disk cache.

See also
setMaximumCacheSize()
QNetworkCacheMetaData QNetworkDiskCache::metaData ( const QUrl url)
overridevirtual
QIODevice * QNetworkDiskCache::prepare ( const QNetworkCacheMetaData metaData)
overridevirtual
bool QNetworkDiskCache::remove ( const QUrl url)
overridevirtual

Reimplemented from QAbstractNetworkCache::remove().

Implements QAbstractNetworkCache.

void QNetworkDiskCache::setCacheDirectory ( const 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.

See also
cacheDirectory(), QDesktopServices::CacheLocation
void QNetworkDiskCache::setMaximumCacheSize ( qint64  size)

Sets the maximum size of the disk cache to be size in bytes.

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

See also
maximumCacheSize()
void QNetworkDiskCache::updateMetaData ( const QNetworkCacheMetaData metaData)
overridevirtual