0

I have an app that needs to be improved by adding disk cache to it. Because it will be deployed on different devices in the future. I have tested the current version with memory caching on a test device. As expected the first load of a specific page took around 100+ milliseconds. Afterwards it decreased to 50-80 seconds due to memory caching. So my next approach was to improve the performance by adding disk-cache. I have referred to this documentation for how to do this. But after adding disk-cache I don't see any improvement of initial loading time compared to the earlier version of the app.

Can anyone point out what am I doing wrong with my approach? The relevant code snippet is given below.

WebPageCheck.h

bool                    m_bInitialized;             ///< state if the CWMWebPageCheck has been initialized
QUrl                    m_oUrl;                     ///< the url which has to be scanned
QStringList             m_oRequests;                ///< list of requests
QNetworkAccessManager  *m_oNetworkManager;          ///< networkmanager to start network requests
//added by Isuru for implementing the diskcache
QNetworkDiskCache      *m_oDiskCache;
CHashReplyManager       m_oReplyManager;            ///< reply manager which handles the network replies

WebPageCheck.cpp

CWMWebPageCheck::CWMWebPageCheck(QUrl oUrl)
{
m_bInitialized = false;
m_oUrl = oUrl;
qDebug() << "Starting WebPageCheck with url: " << oUrl;

m_oRequests << "scripts/scripts_WM.js";
m_oRequests << "screens_wm/logbook_wm.html";

m_oNetworkManager = new QNetworkAccessManager();
m_oDiskCache = new QNetworkDiskCache();
m_oDiskCache->setCacheDirectory(QCoreApplication::applicationDirPath() + "/cacheMachine");
m_oNetworkManager->setCache(m_oDiskCache);
connect(m_oNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onReplyFinished(QNetworkReply*)));
connect(&m_oReplyManager, SIGNAL(sigCalculationError(Te_SealRequest)), this, SLOT(onSigCalculationError(Te_SealRequest)));
connect(&m_oReplyManager, SIGNAL(sigCalculationFinished(Te_SealRequest,QByteArray)), this, SLOT(onSigCalculationFinished(Te_SealRequest,QByteArray)));

m_oReplyManager.setCount(m_oRequests.length());
}

It would be really helpful if someone can list the steps that I have to take to enable disk cache using Qt explaining the use of QNetworkRequest.

Isuru
  • 430
  • 5
  • 21
  • 1
    Have you set Cache attribute? Have you checked there the response came from cache or not? Does reqeusted WebSide support caching??? It should be defined in http header coming from webserver. If caching is explicitly forbidden from website/server Qt will not cache or cache only the minimum amount of static elements – Xplatforms Oct 24 '17 at 12:48
  • @Xplatforms : Thanks for the hints. I think the problem lies somewhere close to QNetworkRequest. I have checked everywhere but I didn't see a clear example of how to implement diskCache using Qt. The documentation which I mentioned is the best I could find so far. But still I'm puzzled how to properly configure this QnetworkRequest thingy... – Isuru Oct 24 '17 at 13:32
  • try to set attribute on QNetworkRequest and look at result in finished slot. QNetworkRequest request2(QUrl(QString("http://qt.nokia.com"))); request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache); manager->get(request2); void replyFinished(QNetworkReply *reply) { QVariant fromCache = reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute); qDebug() << "page from cache?" << fromCache.toBool(); } What does it returns? – Xplatforms Oct 24 '17 at 14:46

0 Answers0