Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkDiskCache not caching
Qt 6.11 is out! See what's new in the release blog

QNetworkDiskCache not caching

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    goocreations
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goocreations
      wrote on last edited by
      #2

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

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved