QtWebKit缓存似乎不起作用

时间:2014-02-04 21:34:24

标签: browser-cache cache-control qtwebkit

所以我写了这个类覆盖基础QWebPage。

这是Qt4.8.5。出于各种原因,我无法真正升级到更新的版本......无论如何,4.4中已经引入了缓存类,所以我应该没问题。

这是根据以下文档编写的:http://blog.qt.digia.com/blog/2011/04/29/http-caching-with-qt/

MyQWebPage::MyQWebPage(QObject* parent /*= nullptr*/) : QWebPage(parent)
{
    QWebHistory *history = this->history();
    history->setMaximumItemCount(0);

    QNetworkAccessManager * pNAM = networkAccessManager();
    if (pNAM) {
        QNetworkDiskCache * pNDC = new QNetworkDiskCache(parent);
        pNDC->setCacheDirectory( QDir::homePath() + "/AppData/Roaming/Autodesk/ash_web_cache" );

        qint64 size = pNDC->cacheSize();
        printf( "cache size = %ld\n", size );

        size = pNDC->maximumCacheSize();
        printf( "maximum cache size = %ld\n", size );

        const qint64 desired = 1024*1024*1024;

        if (size < desired) {
            pNDC->setMaximumCacheSize(desired);
            size = pNDC->maximumCacheSize();
            printf( "new maximum cache size = %ld\n", size );
        }

        QString dir = pNDC->cacheDirectory();
        printf( "cache directory = %s\n", (const char*)dir.toUtf8() );

        pNAM->setCache(pNDC);
    }
}

我现在正在提供1GB的缓存,以确保我正在做的事情应该正确缓存。

所以我的webapp出现了,我打开检查员,进入网络选项卡。 我的网页显示了一堆缩略图。 当我重新加载页面时,虽然应该有足够的空间,但我看不到很多缓存!

如果我在chrome中做同样的事情,我的所有缩略图都会得到200-OK-fromcache,正如预期的那样! 因此,日期,etag等没有问题,因为它全部使用chrome。

但在这个qt网络应用中,不,甚至不是304 ......一切都是200无缓存。

上面代码中的printf行将打印:

cache size = 8342691

maximum cache size = 52428800

new maximum cache size = 1073741824

8342691的高速缓存大小正是我在setCacheDirectory中设置的目录的大小(正好,其子目录之一称为data7)... 但我无法在那里找到我的缩略图。

我有什么问题吗?

BTW我在QtWebSettings中注意到了这个函数:

void QWebSettings::setObjectCacheCapacities(int cacheMinDeadCapacity, int cacheMaxDead, int totalCapacity) [static]

我还没有测试过这个......

还有我可以调整的缓存控件:

enum QNetworkRequest::CacheLoadControl
Controls the caching mechanism of QNetworkAccessManager.
QNetworkRequest::AlwaysNetwork  0   always load from network and do not check if the cache has a valid entry (similar to the "Reload" feature in browsers); in addition, force intermediate caches to re-validate.
QNetworkRequest::PreferNetwork  1   default value; load from the network if the cached entry is older than the network entry. This will never return stale data from the cache, but revalidate resources that have become stale.
QNetworkRequest::PreferCache    2   load from cache if available, otherwise load from network. Note that this can return possibly stale (but not expired) items from cache.
QNetworkRequest::AlwaysCache    3   only load from cache, indicating error if the item was not cached (i.e., off-line mode)

但默认AlwaysNetwork应该没问题,所以我认为我不需要改变它。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。这是文档的错误,但不是代码逻辑。

请将此行添加到您的代码中。 setNetworkAccessManager(pNAM);

MyQWebPage::MyQWebPage(QObject* parent /*= nullptr*/) : QWebPage(parent)
{
QWebHistory *history = this->history();
history->setMaximumItemCount(0);

QNetworkAccessManager * pNAM = networkAccessManager();
if (pNAM) {
    setNetworkAccessManager(pNAM);
    QNetworkDiskCache * pNDC = new QNetworkDiskCache(parent);
    pNDC->setCacheDirectory( QDir::homePath() + "/AppData/Roaming/Autodesk/ash_web_cache" );

    qint64 size = pNDC->cacheSize();
    printf( "cache size = %ld\n", size );

    size = pNDC->maximumCacheSize();
    printf( "maximum cache size = %ld\n", size );

    const qint64 desired = 1024*1024*1024;

    if (size < desired) {
        pNDC->setMaximumCacheSize(desired);
        size = pNDC->maximumCacheSize();
        printf( "new maximum cache size = %ld\n", size );
    }

    QString dir = pNDC->cacheDirectory();
    printf( "cache directory = %s\n", (const char*)dir.toUtf8() );

    pNAM->setCache(pNDC);
}

}

它对我有用。

相关问题