SmokeAPI/src/common/app_cache.cpp

53 lines
1.4 KiB
C++
Raw Normal View History

2023-01-13 00:49:07 +00:00
#include <common/app_cache.hpp>
2023-01-07 22:00:32 +00:00
#include <core/paths.hpp>
#include <koalabox/cache.hpp>
#include <koalabox/logger.hpp>
constexpr auto KEY_APPS = "apps";
2023-01-09 00:26:42 +00:00
AppDlcNameMap get_cached_apps() noexcept {
2023-01-07 22:00:32 +00:00
try {
2023-01-08 08:52:23 +00:00
const auto cache = koalabox::cache::read_from_cache(KEY_APPS);
2023-01-07 22:00:32 +00:00
2023-01-09 00:26:42 +00:00
return cache.get<AppDlcNameMap>();
2023-01-07 22:00:32 +00:00
} catch (const Exception& e) {
2023-01-08 08:52:23 +00:00
LOG_WARN("Failed to get cached apps: {}", e.what())
2023-01-07 22:00:32 +00:00
return {};
}
}
namespace smoke_api::app_cache {
2023-01-09 00:26:42 +00:00
Vector<DLC> get_dlcs(AppId_t app_id) noexcept {
2023-01-07 22:00:32 +00:00
try {
2023-01-08 08:52:23 +00:00
LOG_DEBUG("Reading cached DLC IDs for the app: {}", app_id)
2023-01-07 22:00:32 +00:00
2023-01-09 00:26:42 +00:00
const auto apps = get_cached_apps();
2023-01-07 22:00:32 +00:00
2023-01-09 00:26:42 +00:00
return DLC::get_dlcs_from_apps(apps, app_id);
2023-01-07 22:00:32 +00:00
} catch (const Exception& e) {
2023-01-09 00:26:42 +00:00
LOG_ERROR("Error reading DLCs from disk cache: ", e.what())
2023-01-07 22:00:32 +00:00
return {};
}
}
2023-01-09 00:26:42 +00:00
bool save_dlcs(AppId_t app_id, const Vector<DLC>& dlcs) noexcept {
2023-01-07 22:00:32 +00:00
try {
2023-01-08 08:52:23 +00:00
LOG_DEBUG("Caching DLC IDs for the app: {}", app_id)
2023-01-07 22:00:32 +00:00
auto apps = get_cached_apps();
2023-01-09 00:26:42 +00:00
apps[std::to_string(app_id)] = App{.dlcs=DLC::get_dlc_map_from_vector(dlcs)};
2023-01-07 22:00:32 +00:00
2023-01-08 08:52:23 +00:00
return koalabox::cache::save_to_cache(KEY_APPS, Json(apps));
2023-01-07 22:00:32 +00:00
} catch (const Exception& e) {
2023-01-09 00:26:42 +00:00
LOG_ERROR("Error saving DLCs to disk cache: {}", e.what())
2023-01-07 22:00:32 +00:00
return false;
}
}
}