本文整理汇总了C++中QNetworkCacheMetaData::setSaveToDisk方法的典型用法代码示例。如果您正苦于以下问题:C++ QNetworkCacheMetaData::setSaveToDisk方法的具体用法?C++ QNetworkCacheMetaData::setSaveToDisk怎么用?C++ QNetworkCacheMetaData::setSaveToDisk使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QNetworkCacheMetaData
的用法示例。
在下文中一共展示了QNetworkCacheMetaData::setSaveToDisk方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: injectFakeData
// This function simulates a partially or fully occupied disk cache
// like a normal user of a cache might encounter is real-life browsing.
// The point of this is to trigger degradation in file-system and media performance
// that occur due to the quantity and layout of data.
void tst_qnetworkdiskcache::injectFakeData()
{
QNetworkCacheMetaData::RawHeaderList headers;
headers.append(qMakePair(QByteArray("X-TestHeader"),QByteArray("HeaderValue")));
//Prep cache dir with fake data using QNetworkDiskCache APIs
for (quint32 i = 0; i < NumFakeCacheObjects; i++) {
//prepare metata for url
QNetworkCacheMetaData meta;
QString fakeURL;
QTextStream stream(&fakeURL);
stream << fakeURLbase << i;
QUrl url(fakeURL);
meta.setUrl(url);
meta.setRawHeaders(headers);
meta.setSaveToDisk(true);
//commit payload and metadata to disk
QIODevice *device = cache->prepare(meta);
device->write(payload);
cache->insert(device);
}
}
示例2: timeExpiration
void tst_qnetworkdiskcache::timeExpiration()
{
QFETCH(QString, cacheRootDirectory);
cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
QDir d;
qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
//Housekeeping
initCacheObject();
cleanRecursive(cacheDir); // slow op.
cache->setCacheDirectory(cacheDir);
// Make max cache size HUGE, so that evictions don't happen below
cache->setMaximumCacheSize(qint64(HugeCacheLimit));
cache->clear();
//populate some fake data to simulate partially full cache
injectFakeData();
//Sanity-check that the URL is already in there somewhere
QVERIFY(isUrlCached(NumRemovals-1) == true);
//Entries in the cache should be > what we try to remove
QVERIFY(NumFakeCacheObjects > NumRemovals);
//Set cache limit lower, so this force 1 round of eviction
cache->setMaximumCacheSize(qint64(TinyCacheLimit));
//time insertions of additional content, which is likely to internally cause evictions
QBENCHMARK_ONCE {
for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
//prepare metata for url
QNetworkCacheMetaData meta;
QString fakeURL;
QTextStream stream(&fakeURL);
stream << fakeURLbase << i;//codescanner::leave
QUrl url(fakeURL);
meta.setUrl(url);
meta.setSaveToDisk(true);
//commit payload and metadata to disk
QIODevice *device = cache->prepare(meta);
device->write(payload);
cache->insert(device); // this should trigger evictions, if TinyCacheLimit is small enough
}
}
//Cleanup (slow)
cleanupCacheObject();
cleanRecursive(cacheDir);
}
示例3: metaData
virtual QNetworkCacheMetaData metaData(const QUrl &url)
{
QNetworkCacheMetaData metaData;
if (!cachedData.isEmpty()) {
metaData.setUrl(url);
QDateTime now = QDateTime::currentDateTime();
metaData.setLastModified(now.addDays(-1));
metaData.setExpirationDate(now.addDays(1));
metaData.setSaveToDisk(true);
}
return metaData;
}
示例4: timeInsertion
//This functions times an insert() operation.
//You can run it after populating the cache with
//fake data so that more realistic performance
//estimates are obtained.
void tst_qnetworkdiskcache::timeInsertion()
{
QFETCH(QString, cacheRootDirectory);
cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
QDir d;
qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
//Housekeeping
cleanRecursive(cacheDir); // slow op.
initCacheObject();
cache->setCacheDirectory(cacheDir);
cache->setMaximumCacheSize(qint64(HugeCacheLimit));
cache->clear();
//populate some fake data to simulate partially full cache
injectFakeData(); // SLOW
//Sanity-check that the first URL that we insert below isn't already in there.
QVERIFY(isUrlCached(NumFakeCacheObjects) == false);
// IMPORTANT: max cache size should be HugeCacheLimit, to avoid evictions below
//time insertion of previously-uncached URLs.
QBENCHMARK_ONCE {
for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
//prepare metata for url
QNetworkCacheMetaData meta;
QString fakeURL;
QTextStream stream(&fakeURL);
stream << fakeURLbase << i;
QUrl url(fakeURL);
meta.setUrl(url);
meta.setSaveToDisk(true);
//commit payload and metadata to disk
QIODevice *device = cache->prepare(meta);
device->write(payload);
cache->insert(device);
}
}
//SLOW cleanup
cleanupCacheObject();
cleanRecursive(cacheDir);
}
示例5: saveToCache
bool saveToCache(const QUrl& url, const QByteArray& file) {
if (auto cache = NetworkAccessManager::getInstance().cache()) {
if (!cache->metaData(url).isValid()) {
QNetworkCacheMetaData metaData;
metaData.setUrl(url);
metaData.setSaveToDisk(true);
metaData.setLastModified(QDateTime::currentDateTime());
metaData.setExpirationDate(QDateTime()); // Never expires
// ioDevice is managed by the cache and should either be passed back to insert or remove!
if (auto ioDevice = cache->prepare(metaData)) {
ioDevice->write(file);
cache->insert(ioDevice);
qCDebug(asset_client) << url.toDisplayString() << "saved to disk cache";
return true;
}
qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache.";
}
} else {
qCWarning(asset_client) << "No disk cache to save assets to.";
}
return false;
}