sped up disk cache accesses

Shifted some code around to improve performance
This commit is contained in:
itsmattkc
2021-03-08 11:26:03 +11:00
parent a57bf35e93
commit 8ef608c246
2 changed files with 35 additions and 37 deletions
+33 -35
View File
@@ -203,12 +203,14 @@ bool DiskCacheFolder::ClearCache()
{ {
bool deleted_files = true; bool deleted_files = true;
std::list<HashTime>::iterator i = disk_data_.begin(); auto i = disk_data_.begin();
while (i != disk_data_.end()) { while (i != disk_data_.end()) {
// We return a false result if any of the files fail to delete, but still try to delete as many as we can // We return a false result if any of the files fail to delete, but still try to delete as many as we can
if (QFile::remove(i->file_name) || !QFileInfo::exists(i->file_name)) { const HashTime& ht = i.value();
emit DeletedFrame(path_, i->hash);
if (QFile::remove(ht.file_name) || !QFileInfo::exists(ht.file_name)) {
emit DeletedFrame(path_, i.key());
i = disk_data_.erase(i); i = disk_data_.erase(i);
} else { } else {
qWarning() << "Failed to delete" << i->file_name; qWarning() << "Failed to delete" << i->file_name;
@@ -222,30 +224,14 @@ bool DiskCacheFolder::ClearCache()
void DiskCacheFolder::Accessed(const QByteArray &hash) void DiskCacheFolder::Accessed(const QByteArray &hash)
{ {
std::list<HashTime>::iterator i = disk_data_.begin(); disk_data_[hash].access_time = QDateTime::currentMSecsSinceEpoch();
while (i != disk_data_.end()) {
if (i->hash == hash) {
// Copy access data and erase from list
HashTime accessed_hash = *i;
disk_data_.erase(i);
// Add it to the end
disk_data_.push_back(accessed_hash);
// End loop
break;
} else {
i++;
}
}
} }
void DiskCacheFolder::CreatedFile(const QString &file_name, const QByteArray &hash) void DiskCacheFolder::CreatedFile(const QString &file_name, const QByteArray &hash)
{ {
qint64 file_size = QFile(file_name).size(); qint64 file_size = QFile(file_name).size();
disk_data_.push_back({file_name, hash, file_size}); disk_data_.insert(hash, {file_name, file_size, QDateTime::currentMSecsSinceEpoch()});
consumption_ += file_size; consumption_ += file_size;
@@ -267,8 +253,8 @@ void DiskCacheFolder::SetPath(const QString &path)
// Signal that disk cache is gone // Signal that disk cache is gone
if (!disk_data_.empty()) { if (!disk_data_.empty()) {
foreach (const HashTime& h, disk_data_) { for (auto it=disk_data_.cbegin(); it!=disk_data_.cend(); it++) {
emit DeletedFrame(path_, h.hash); emit DeletedFrame(path_, it.key());
} }
disk_data_.clear(); disk_data_.clear();
} }
@@ -283,7 +269,7 @@ void DiskCacheFolder::SetPath(const QString &path)
// Attempt to load existing index file from path // Attempt to load existing index file from path
QDir path_dir(path_); QDir path_dir(path_);
path_dir.mkpath("."); path_dir.mkpath(QStringLiteral("."));
index_path_ = path_dir.filePath(QStringLiteral("index")); index_path_ = path_dir.filePath(QStringLiteral("index"));
@@ -297,15 +283,17 @@ void DiskCacheFolder::SetPath(const QString &path)
ds >> clear_on_close_; ds >> clear_on_close_;
while (!cache_index_file.atEnd()) { while (!cache_index_file.atEnd()) {
QByteArray hash;
HashTime h; HashTime h;
ds >> h.file_name; ds >> h.file_name;
ds >> h.hash; ds >> hash;
ds >> h.file_size; ds >> h.file_size;
ds >> h.access_time;
if (QFileInfo::exists(h.file_name)) { if (QFileInfo::exists(h.file_name)) {
consumption_ += h.file_size; consumption_ += h.file_size;
disk_data_.push_back(h); disk_data_.insert(hash, h);
} }
} }
@@ -315,14 +303,21 @@ void DiskCacheFolder::SetPath(const QString &path)
QByteArray DiskCacheFolder::DeleteLeastRecent() QByteArray DiskCacheFolder::DeleteLeastRecent()
{ {
HashTime h = disk_data_.front(); auto hash_to_delete = disk_data_.begin();
disk_data_.pop_front();
QFile::remove(h.file_name); for (auto it=disk_data_.begin()+1; it!=disk_data_.end(); it++) {
if (hash_to_delete.value().access_time > it.value().access_time) {
hash_to_delete = it;
}
}
consumption_ -= h.file_size; QByteArray hash = hash_to_delete.key();
HashTime ht = hash_to_delete.value();
disk_data_.erase(hash_to_delete);
return h.hash; consumption_ -= ht.file_size;
return hash;
} }
void DiskCacheFolder::CloseCacheFolder() void DiskCacheFolder::CloseCacheFolder()
@@ -351,10 +346,13 @@ void DiskCacheFolder::SaveDiskCacheIndex()
ds << limit_; ds << limit_;
ds << clear_on_close_; ds << clear_on_close_;
foreach (const HashTime& h, disk_data_) { for (auto it=disk_data_.cbegin(); it!=disk_data_.cend(); it++) {
ds << h.file_name; const HashTime& ht = it.value();
ds << h.hash;
ds << h.file_size; ds << ht.file_name;
ds << it.key();
ds << ht.file_size;
ds << ht.access_time;
} }
cache_index_file.close(); cache_index_file.close();
+2 -2
View File
@@ -86,11 +86,11 @@ private:
struct HashTime { struct HashTime {
QString file_name; QString file_name;
QByteArray hash;
qint64 file_size; qint64 file_size;
qint64 access_time;
}; };
std::list<HashTime> disk_data_; QMap<QByteArray, HashTime> disk_data_;
qint64 consumption_; qint64 consumption_;