Files
oak-editor/app/render/diskmanager.cpp
T
itsmattkc 362cb9daef made auto-deleting cache on close an option
Previously we had no disk management whatsoever, so we cleared the cache on
every close just to prevent clogging up tester disk space. Now that we are
implementing disk management, there are better things to do on close regarding
disk cache. However, some users may still wish for the app to delete the cache
on close, so it's provided as an option.
2020-01-09 21:00:04 +11:00

127 lines
2.5 KiB
C++

#include "diskmanager.h"
#include <QDataStream>
#include <QDateTime>
#include <QDir>
#include <QFile>
#include "common/filefunctions.h"
#include "config/config.h"
DiskManager* DiskManager::instance_ = nullptr;
DiskManager::DiskManager() :
consumption_(0)
{
// Try to load any current cache index from file
QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index"));
if (cache_index_file.open(QFile::ReadOnly)) {
QDataStream ds(&cache_index_file);
ds >> consumption_;
while (!cache_index_file.atEnd()) {
HashTime h;
ds >> h.file_name;
ds >> h.hash;
ds >> h.access_time;
ds >> h.file_size;
disk_data_.append(h);
}
}
}
DiskManager::~DiskManager()
{
if (Config::Current()["ClearDiskCacheOnClose"].toBool()) {
// Clear all cache data
QDir(GetMediaCacheLocation()).removeRecursively();
} else {
// Save current cache index
QFile cache_index_file(QDir(GetMediaCacheLocation()).filePath("index"));
if (cache_index_file.open(QFile::WriteOnly)) {
QDataStream ds(&cache_index_file);
ds << consumption_;
foreach (const HashTime& h, disk_data_) {
ds << h.file_name;
ds << h.hash;
ds << h.access_time;
ds << h.file_size;
}
} else {
qWarning() << "Failed to write cache index";
}
}
}
void DiskManager::CreateInstance()
{
instance_ = new DiskManager();
}
void DiskManager::DestroyInstance()
{
delete instance_;
instance_ = nullptr;
}
DiskManager *DiskManager::instance()
{
return instance_;
}
void DiskManager::Accessed(const QByteArray &hash)
{
for (int i=disk_data_.size()-1;i>=0;i--) {
const HashTime& h = disk_data_.at(i);
if (h.hash == hash) {
HashTime moved_hash = h;
moved_hash.access_time = QDateTime::currentMSecsSinceEpoch();
disk_data_.removeAt(i);
disk_data_.append(moved_hash);
break;
}
}
}
void DiskManager::CreatedFile(const QString &file_name, const QByteArray &hash)
{
qint64 file_size = QFile(file_name).size();
disk_data_.append({file_name, hash, QDateTime::currentMSecsSinceEpoch(), });
consumption_ += file_size;
while (consumption_ > DiskLimit()) {
DeleteLeastRecent();
}
}
void DiskManager::DeleteLeastRecent()
{
HashTime h = disk_data_.takeFirst();
QFile::remove(h.file_name);
consumption_ -= h.file_size;
emit DeletedFrame(h.hash);
}
qint64 DiskManager::DiskLimit()
{
double gigabytes = Config::Current()["DiskCacheSize"].toDouble();
// Convert gigabytes to bytes
return qRound64(gigabytes * 1073741824);
}