当当前的cache文件大小之和大于MAX_DISKCACHE_SIZE的90%时,older
cache文件会被删除直到cache文件大小之和小于MAX_DISKCACHE_SIZE的90%,其余的10%相当于备用,这样就不会出现某一条
cache记录装不进去的现象,除非MAX_DISKCACHE_SIZE非常小。这里的older
cache是由系统时钟决定的。cache被创建时会有一个时间信息,而当被引用时,不会另外去创建新的cache文件,而仅仅是更新该cache文件的时间信息。那些时间信息越早的cache文件会被优先淘汰。
qint64 QNetworkDiskCache::expire()
{
Q_D(QNetworkDiskCache);
if (d->currentCacheSize >= 0 && d->currentCacheSize < maximumCacheSize())
return d->currentCacheSize;
if (cacheDirectory().isEmpty()) {
qWarning() << "QNetworkDiskCache::expire() The cache directory is not set";
return 0;
}
QDir::Filters filters = QDir::AllDirs | QDir:: Files | QDir::NoDotAndDotDot;
QDirIterator it(cacheDirectory(), filters, QDirIterator::Subdirectories);
QMultiMap cacheItems;
qint64 totalSize = 0;
while (it.hasNext()) {
QString path = it.next();
QFileInfo info = it.fileInfo();
QString fileName = info.fileName();
if (fileName.endsWith(CACHE_POSTFIX) && fileName.startsWith(CACHE_PREFIX)) {
cacheItems.insert(info.created(), path);
totalSize += info.size();
}
}
int removedFiles = 0;
qint64 goal = (maximumCacheSize() * 9) / 10;
QMultiMap::const_iterator i = cacheItems.constBegin();
while (i != cacheItems.constEnd()) {
if (totalSize < goal)
break;
QString name = i.value();
QFile file(name);
qint64 size = file.size();
file.remove();
totalSize -= size;
++removedFiles;
++i;
}
#if defined(QNETWORKDISKCACHE_DEBUG)
if (removedFiles > 0) {
qDebug() << "QNetworkDiskCache::expire()"
<< "Removed:" << removedFiles
<< "Kept:" << cacheItems.count() - removedFiles;
}
#endif
if (removedFiles > 0)
d->lastItem.reset();
return totalSize;
}
阅读(3343) | 评论(0) | 转发(0) |