1

我正在尝试为我的应用启用磁盘缓存以获得更好的用户体验。我将此文档用作此指南。我最初认为我配置它正确,因为我没有收到任何错误,但后来看到我的磁盘缓存文件夹是空的。然后我到处搜索并尝试了很多东西,但无法解决这个问题。以下是相关的代码片段,

CWMWebPageCheck.h

class CWMWebPageCheck : public QObject
{
        Q_OBJECT
    public:
    /// \brief Constructor for a CWMWebPageCheck object
    ///
    /// \param oUrl
    CWMWebPageCheck(QUrl oUrl);
    virtual ~CWMWebPageCheck();
    void  initialize();
    void  initiateChecksumCalculation();
    void  resetHash();
    ...
    protected:
        ///
        /// \brief Reads the hash out of database.
        ///
        void                    hashRead();

        ///
        /// \brief Writes the hash into database.
        ///
        void                    hashWrite();



        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


        QByteArray              m_oHash;                    ///< the current hash




        void                    initiateDataTransfer();


 };

 #endif // CWMWEBPAGECHECK_

CWMWebPageCheck.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(this);
    m_oDiskCache = new QNetworkDiskCache(this);
    //m_oDiskCache->setMaximumCacheSize(1000000); //set the cache to 10megs
    //QString location = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); //QStandardPath is not included in the Qt4.8
    m_oDiskCache->setCacheDirectory(QCoreApplication::applicationDirPath() + "/Disk_cache");
    //m_oDiskCache->setCacheDirectory(location);
    m_oNetworkManager->setCache(m_oDiskCache);
    //m_oDiskCache->maximumCacheSize(); //Returns the current maximum size in bytes for the disk cache

    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());
}

//Now comes the part where I think the problem comes from
void CWMWebPageCheck::initiateDataTransfer()
{


  qDebug("*******inside initiateDataTransfer()");
for (QString oPath: m_oRequests)
{

    QString sUrl = m_oUrl.toString();
    // do a normal request (preferred from network, as this is the default)
 //   QNetworkRequest request(QUrl(QString("%1/%2").arg(sUrl).arg(oPath)));
 //   m_oNetworkManager->get(request);

    // do a request preferred from cache
    QNetworkRequest request2(QUrl(QString("%1/%2").arg(sUrl).arg(oPath)));
    request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
    m_oNetworkManager->get(request2);
   /* QString sUrl = m_oUrl.toString();
   // m_oNetworkManager->get(QNetworkRequest(QString("%1/%2").arg(sUrl).arg(oPath)).setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache));

    //#################################################################################################
   /* QNetworkRequest request(QUrl(QString("%1/%2").arg(sUrl).arg(oPath)));
    *
    request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
    m_oNetworkManager->get(request);*/

    /*QNetworkRequest request = QNetworkRequest();

    request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
    request.setUrl(QUrl(QString("%1/%2").arg(sUrl).arg(oPath)));
    m_oNetworkManager->get(request);*/
   // qDebug("***********====== GOT REQUET=======***************");
// do a request preferred from cache
     /*   QNetworkRequest request2(QUrl(QString("%1/%2").arg(sUrl).arg(oPath)));
        request2.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
        m_oNetworkManager->get(request2);
        qDebug("***********====== GOT REQUET=======***************");*/
        //#################################################################################################
    }

我在这里做错了什么?提前致谢!

4

0 回答 0