QNetworkDiskCache not caching

  • I implemented a QNetworkDiskCache to cache files that are downloaded from my server. I used the example code in the documentation (http://doc.qt.io/qt-4.8/qnetworkdiskcache.html). However, every time a request a file, the request is retrieved from the network instead of the cache, or that is at least what is outputted here:

    void Cacher::requestFinished(QNetworkReply* reply)
    	QByteArray data=reply->readAll();
    	QString str(data);
    	qDebug() << "---------------------------------------------------";
    	qDebug() << "Size:" << data.size();
    	qDebug() << "Item Url:" << reply->url().toString();
    	qDebug() << "Content Type:" << reply->header(QNetworkRequest::ContentTypeHeader).toString();
    	QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute);
    	qDebug() << "Item from cache?" << fromCache.toBool();
    	qDebug() << "File Last Modified:" << reply->header(QNetworkRequest::LastModifiedHeader).toString();

    And yes, I've set

    request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);

    I'm unsure why the file is never retrieved from the cache, so I set the following HTTP headers, just in case they are needed:

    Cache-Control: max-age=31556926, private
    Expires: 0

    Does someone know why my files are never cached? Is there something else I have to set?

  • Seems that my Apache server automatically set the HTTP header to Pragma: no-cache. Changed that and now caching is working.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.