cache champdata

This commit is contained in:
mrbesen 2022-07-05 23:45:28 +02:00
parent 5dd822d332
commit e2f04637f2
Signed by untrusted user: MrBesen
GPG Key ID: 596B2350DCD67504
8 changed files with 97 additions and 4 deletions

19
include/champcache.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <QJsonDocument>
// This file caches the champion metadata
class ChampCache {
public:
ChampCache();
std::string getVersion();
QJsonDocument getChamps();
void saveChamps(QJsonDocument doc, const std::string& version);
private:
std::string basefolder;
uint64_t maxage = 86400; // is in seconds
};

View File

@ -11,6 +11,7 @@
#include <QPixmap>
#include "datadragonimagecache.h"
#include "champcache.h"
#include "memoryimagecache.h"
#include "restclient.h"
@ -83,6 +84,7 @@ private:
DataDragonImageCache cache[3];
ChampCache champCache;
MemoryImageCache memcache;
std::list<Task> tasks;
std::mutex tasksmutex;

View File

@ -9,3 +9,6 @@ bool mkdirs(const std::string& path);
// get $HOME or a useful default value
std::string getHome();
// folder for caching example: $HOME/.cache/lolautoaccept/
std::string getCache();

View File

@ -28,6 +28,7 @@ defineReplace(prependAll) {
SOURCES += \
src/arg.cpp \
src/champcache.cpp \
src/championsearch.cpp \
src/champrow.cpp \
src/clientaccess.cpp \
@ -51,6 +52,7 @@ SOURCES += \
HEADERS += \
include/arg.h \
include/champcache.h \
include/championsearch.h \
include/champrow.h \
include/clientaccess.h \

54
src/champcache.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "champcache.h"
#include "files.h"
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <Log.h>
ChampCache::ChampCache() {
basefolder = getCache();
}
// the age of f in seconds
static qint64 ageOfFile(QFile& f) {
QFileInfo info(f);
return info.lastModified().secsTo(QDateTime::currentDateTime());
}
std::string ChampCache::getVersion() {
QFile versionfile(QString::fromStdString(basefolder + "version"));
if(ageOfFile(versionfile) < (qint64) maxage) {
versionfile.open(QFile::ReadOnly);
return versionfile.readAll().toStdString();
}
return {}; // empty string
}
QJsonDocument ChampCache::getChamps() {
QFile champsfile(QString::fromStdString(basefolder + "champs.json"));
if(ageOfFile(champsfile) < (qint64) maxage) {
champsfile.open(QFile::ReadOnly);
QByteArray bytes = champsfile.readAll();
QJsonDocument doc = QJsonDocument::fromJson(bytes);
return doc;
}
return {}; // empty document
}
void ChampCache::saveChamps(QJsonDocument doc, const std::string& version) {
QByteArray arr = doc.toJson();
QFile champsfile(QString::fromStdString(basefolder + "champs.json"));
champsfile.open(QFile::WriteOnly | QFile::Truncate);
champsfile.write(arr);
QFile versionfile(QString::fromStdString(basefolder + "version"));
versionfile.open(QFile::WriteOnly | QFile::Truncate);
versionfile.write(version.c_str());
versionfile.close();
Log::info << "saved Champs and version Cache";
}

View File

@ -245,6 +245,9 @@ void DataDragon::getVersionInternal() {
if(!version.empty()) return;
version = champCache.getVersion();
if(!version.empty()) return;
QJsonDocument jversions = request("api/versions.json");
if(jversions.isArray()) {
QJsonArray jverarr = jversions.array();
@ -266,13 +269,19 @@ void DataDragon::getChampsInternal() {
return;
}
QJsonDocument jchamps = request(getCDNString() + "data/" + locale + "/champion.json");
QJsonDocument jchamps = champCache.getChamps();
if(jchamps.isEmpty()) {
// try again with default locale
locale = "en_US";
jchamps = request(getCDNString() + "data/" + locale + "/champion.json");
if(jchamps.isEmpty()) {
// try again with default locale
locale = "en_US";
jchamps = request(getCDNString() + "data/" + locale + "/champion.json");
}
}
if(jchamps.isObject()) {
// save to cache
champCache.saveChamps(jchamps, version);
QJsonObject obj = jchamps.object();
auto it = obj.constFind("data");
if(it != obj.constEnd() && it.value().isObject()) {

View File

@ -9,7 +9,7 @@
DataDragonImageCache::DataDragonImageCache(const std::string& folderextra, const std::string& imageext) : imageext(imageext) {
// init cache dir
cacheDir = getHome() + ".cache/lolautoaccept/" + folderextra + "/";
cacheDir = getCache() + folderextra + "/";
mkdirs(cacheDir);
}

View File

@ -26,4 +26,8 @@ std::string getHome() {
return "./";
}
return std::string(homevar) + "/";
}
std::string getCache() {
return getHome() + ".cache/lolautoaccept/";
}