Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Network caching in Qt

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Tested on Nokia N96,
Nokia 6210 Navigator
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition, FP2
S60 5th Edition
Symbian
S60 5th Edition
S60 3rd Edition FP2
Article
Keywords: QNetworkDiskCache
Created: User:Kbwiki (30 Nov 2009)
Last edited: hamishwillee (11 Oct 2012)

Overview

This article demonstrates how to use network caching in Qt.

Description

The QNetworkDiskCache class provides a very basic disk cache. It can be used with QNetworkAccessManager to store the requested web pages into a specific directory and get the cached content whenever required. Each cached file has a cache_ prefix and .cache extension in the file name.

Network Caching

Required headers

 #include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkDiskCache>

Required modules

 QT += network

NetWorkCacheTest is our test class, derived from QWidget:

   class NetWorkCacheTest : public QWidget
{
Q_OBJECT
 
NetWorkCacheTest( QWidget *parent = 0 );
// ...
 
public:
void sendRequest();
 
public slots:
// Slot to receive finished signal from QNetworkAccessManager
void finishedSlot( QNetworkReply* aReply );
 
private:
 
QNetworkAccessManager* m_nam;
QNetworkDiskCache* m_diskCache;
QUrl m_url;
};

Constructor implementation

  NetWorkCacheTest::NetWorkCacheTest( QWidget *parent )
: QWidget( parent )
{
...
m_url.setUrl( <nowiki>"http://www.developer.nokia.com/"</nowiki> );
m_nam = new QNetworkAccessManager( this );
 
// Connect the finished signal to receive network replies
connect( m_nam, SIGNAL( finished( QNetworkReply* ) ), this, SLOT( finishedSlot( QNetworkReply* ) ) );
 
// Create an instance of disk cache and set cache directory
m_diskCache = new QNetworkDiskCache( this );
QString sysCacheDir( "c:\\data" );
m_diskCache->setCacheDirectory( sysCacheDir );
 
// Assign cache to network manager and send a request
m_nam->setCache( m_diskCache );
sendRequest();
};

sendRequest() implementation

  void NetWorkCacheTest::sendRequest()
{
QNetworkRequest request( m_url );
 
// Load from cache if available, otherwise load from network, may return stale data
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
 
// Data obtained should be saved to cache for future uses
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
 
// Posts a request to obtain the contents of the target request,
// finished signal will get triggered when network reply is available
m_nam->get( QNetworkRequest( request ) );
}

Handling the network reply

  void NetWorkCacheEx::finishedSlot( QNetworkReply* reply )
{
 
// Get and display the last modified header
QVariant lmod = reply->header( QNetworkRequest::LastModifiedHeader );
if ( lmod.isValid() ) {
QDateTime lmodDate = lmod.toDateTime();
QString lmodStr = lmodDate.toString( "dd.MM.yyyy" );
QMessageBox::information( this, "Last Modified", lmodStr, "ok" );
}
 
// Show expiry date
if ( reply->hasRawHeader( "Expires" ) ) {
QString expires = reply->rawHeader( "Expires" );
QMessageBox::information( this, "Expiry date", expires, "ok" );
}
 
// Show Cache-Control header
if ( reply->hasRawHeader( "Cache-Control" ) ) {
QString cCntrl = reply->rawHeader( "Cache-Control" );
QMessageBox::information( this, "Cache-Control", cCntrl, "ok" );
}
 
// Check if the data was obtained from cache or not
QVariant fromCache = reply->attribute( QNetworkRequest::SourceIsFromCacheAttribute );
if ( fromCache.toBool() ) {
// Data is from cache
}
else {
// Data is from network
}
 
if ( reply->error() == QNetworkReply::NoError ) {
// Display received data in web view (needs webkit module added)
// QWebView view;
// Reading bytes form the reply
// QByteArray bytes = reply->readAll();
// view.setHtml(bytes);
// view.show();
}
else { // Error handling
int error = reply->error();
QMessageBox::information( this, "Error", reply->errorString(), "ok" );
}
}
This page was last modified on 11 October 2012, at 01:17.
109 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×