Cache store results
This commit is contained in:
parent
d714b98dee
commit
2ec89ee1eb
5 changed files with 141 additions and 24 deletions
2
backend/Cargo.lock
generated
2
backend/Cargo.lock
generated
|
@ -304,6 +304,7 @@ dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"serde",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"windows-targets 0.48.5",
|
"windows-targets 0.48.5",
|
||||||
]
|
]
|
||||||
|
@ -1172,6 +1173,7 @@ name = "powertools"
|
||||||
version = "1.5.0-ng1"
|
version = "1.5.0-ng1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"community_settings_core",
|
"community_settings_core",
|
||||||
"libc",
|
"libc",
|
||||||
|
|
|
@ -28,12 +28,20 @@ simplelog = "0.12"
|
||||||
|
|
||||||
# limits & driver functionality
|
# limits & driver functionality
|
||||||
limits_core = { version = "3", path = "./limits_core" }
|
limits_core = { version = "3", path = "./limits_core" }
|
||||||
community_settings_core = { version = "0.1", path = "./community_settings_core" }
|
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
|
||||||
|
# steam deck libs
|
||||||
smokepatio = { version = "0.1", features = [ "std" ], path = "../../smokepatio" }
|
smokepatio = { version = "0.1", features = [ "std" ], path = "../../smokepatio" }
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
|
|
||||||
|
# online settings
|
||||||
|
community_settings_core = { version = "0.1", path = "./community_settings_core" }
|
||||||
|
chrono = { version = "0.4", features = [ "serde" ] }
|
||||||
|
|
||||||
|
# hardware enablement
|
||||||
#libryzenadj = { version = "0.14", path = "../../libryzenadj-rs-14" }
|
#libryzenadj = { version = "0.14", path = "../../libryzenadj-rs-14" }
|
||||||
libryzenadj = { version = "0.13" }
|
libryzenadj = { version = "0.13" }
|
||||||
|
|
||||||
# ureq's tls feature does not like musl targets
|
# ureq's tls feature does not like musl targets
|
||||||
ureq = { version = "2", features = ["json", "gzip", "brotli", "charset"], default-features = false, optional = true }
|
ureq = { version = "2", features = ["json", "gzip", "brotli", "charset"], default-features = false, optional = true }
|
||||||
|
|
||||||
|
|
|
@ -3,11 +3,26 @@ use std::sync::{Arc, Mutex, RwLock};
|
||||||
use usdpl_back::core::serdes::Primitive;
|
use usdpl_back::core::serdes::Primitive;
|
||||||
use usdpl_back::AsyncCallable;
|
use usdpl_back::AsyncCallable;
|
||||||
|
|
||||||
|
use chrono::{offset::Utc, DateTime};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use super::handler::{ApiMessage, GeneralMessage};
|
use super::handler::{ApiMessage, GeneralMessage};
|
||||||
|
|
||||||
const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us";
|
const BASE_URL_FALLBACK: &'static str = "https://powertools.ngni.us";
|
||||||
static BASE_URL: RwLock<Option<String>> = RwLock::new(None);
|
static BASE_URL: RwLock<Option<String>> = RwLock::new(None);
|
||||||
|
|
||||||
|
const MAX_CACHE_DURATION: std::time::Duration =
|
||||||
|
std::time::Duration::from_secs(60 * 60 * 24 * 7 /* 7 days */);
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
|
struct CachedData<T> {
|
||||||
|
data: T,
|
||||||
|
updated: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
type StoreCache =
|
||||||
|
std::collections::HashMap<u32, CachedData<Vec<community_settings_core::v1::Metadata>>>;
|
||||||
|
|
||||||
pub fn set_base_url(base_url: String) {
|
pub fn set_base_url(base_url: String) {
|
||||||
*BASE_URL
|
*BASE_URL
|
||||||
.write()
|
.write()
|
||||||
|
@ -34,35 +49,124 @@ fn url_upload_config() -> String {
|
||||||
format!("{}/api/setting", get_base_url())
|
format!("{}/api/setting", get_base_url())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cache_path() -> std::path::PathBuf {
|
||||||
|
crate::utility::settings_dir().join(crate::consts::WEB_SETTINGS_CACHE)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_cache() -> StoreCache {
|
||||||
|
let path = cache_path();
|
||||||
|
let file = match std::fs::File::open(&path) {
|
||||||
|
Ok(f) => f,
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Failed to open store cache {}: {}", path.display(), e);
|
||||||
|
return StoreCache::default();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut file = std::io::BufReader::new(file);
|
||||||
|
match ron::de::from_reader(&mut file) {
|
||||||
|
Ok(cache) => cache,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to parse store cache {}: {}", path.display(), e);
|
||||||
|
return StoreCache::default();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_cache(cache: &StoreCache) {
|
||||||
|
let path = cache_path();
|
||||||
|
let file = match std::fs::File::create(&path) {
|
||||||
|
Ok(f) => f,
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Failed to create store cache {}: {}", path.display(), e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut file = std::io::BufWriter::new(file);
|
||||||
|
if let Err(e) =
|
||||||
|
ron::ser::to_writer_pretty(&mut file, cache, crate::utility::ron_pretty_config())
|
||||||
|
{
|
||||||
|
log::error!("Failed to parse store cache {}: {}", path.display(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_maybe_cached(steam_app_id: u32) -> Vec<community_settings_core::v1::Metadata> {
|
||||||
|
let mut cache = load_cache();
|
||||||
|
if let Some(cached_result) = cache.get(&steam_app_id) {
|
||||||
|
if cached_result.updated < (Utc::now() - MAX_CACHE_DURATION) {
|
||||||
|
// cache needs update
|
||||||
|
if let Ok(result) = search_by_app_id_online(steam_app_id) {
|
||||||
|
cache.insert(
|
||||||
|
steam_app_id,
|
||||||
|
CachedData {
|
||||||
|
data: result.clone(),
|
||||||
|
updated: Utc::now(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
save_cache(&cache);
|
||||||
|
result
|
||||||
|
} else {
|
||||||
|
// if all else fails, out of date results are better than no results
|
||||||
|
cached_result.data.to_owned()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// cache is ok, use it
|
||||||
|
cached_result.data.to_owned()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if let Ok(result) = search_by_app_id_online(steam_app_id) {
|
||||||
|
cache.insert(
|
||||||
|
steam_app_id,
|
||||||
|
CachedData {
|
||||||
|
data: result.clone(),
|
||||||
|
updated: Utc::now(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
save_cache(&cache);
|
||||||
|
result
|
||||||
|
} else {
|
||||||
|
Vec::with_capacity(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_by_app_id_online(
|
||||||
|
steam_app_id: u32,
|
||||||
|
) -> std::io::Result<Vec<community_settings_core::v1::Metadata>> {
|
||||||
|
let req_url = url_search_by_app_id(steam_app_id);
|
||||||
|
match ureq::get(&req_url).call() {
|
||||||
|
Ok(response) => {
|
||||||
|
let json_res: std::io::Result<Vec<community_settings_core::v1::Metadata>> =
|
||||||
|
response.into_json();
|
||||||
|
match json_res {
|
||||||
|
Ok(search_results) => Ok(search_results),
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Cannot parse response from `{}`: {}", req_url, e);
|
||||||
|
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Cannot get search results from `{}`: {}", req_url, e);
|
||||||
|
Err(std::io::Error::new(
|
||||||
|
std::io::ErrorKind::ConnectionAborted,
|
||||||
|
e,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Get search results web method
|
/// Get search results web method
|
||||||
pub fn search_by_app_id() -> impl AsyncCallable {
|
pub fn search_by_app_id() -> impl AsyncCallable {
|
||||||
let getter = move || {
|
let getter = move || {
|
||||||
move |steam_app_id: u32| {
|
move |steam_app_id: u32| {
|
||||||
let req_url = url_search_by_app_id(steam_app_id);
|
let search_results = get_maybe_cached(steam_app_id);
|
||||||
match ureq::get(&req_url).call() {
|
match serde_json::to_string(&search_results) {
|
||||||
Ok(response) => {
|
Err(e) => {
|
||||||
let json_res: std::io::Result<Vec<community_settings_core::v1::Metadata>> =
|
log::error!("Cannot convert search results to JSON: {}", e);
|
||||||
response.into_json();
|
"[]".to_owned()
|
||||||
match json_res {
|
|
||||||
Ok(search_results) => {
|
|
||||||
// search results may be quite large, so let's do the JSON string conversion in the background (blocking) thread
|
|
||||||
match serde_json::to_string(&search_results) {
|
|
||||||
Err(e) => log::error!(
|
|
||||||
"Cannot convert search results from `{}` to JSON: {}",
|
|
||||||
req_url,
|
|
||||||
e
|
|
||||||
),
|
|
||||||
Ok(s) => return s,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
log::error!("Cannot parse response from `{}`: {}", req_url, e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => log::warn!("Cannot get search results from `{}`: {}", req_url, e),
|
Ok(s) => s,
|
||||||
}
|
}
|
||||||
"[]".to_owned()
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
super::async_utils::AsyncIsh {
|
super::async_utils::AsyncIsh {
|
||||||
|
|
|
@ -10,4 +10,6 @@ pub const DEFAULT_SETTINGS_VARIANT_NAME: &str = "Primary";
|
||||||
pub const LIMITS_FILE: &str = "limits_cache.ron";
|
pub const LIMITS_FILE: &str = "limits_cache.ron";
|
||||||
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.ron";
|
pub const LIMITS_OVERRIDE_FILE: &str = "limits_override.ron";
|
||||||
|
|
||||||
|
pub const WEB_SETTINGS_CACHE: &str = "store_cache.ron";
|
||||||
|
|
||||||
pub const MESSAGE_SEEN_ID_FILE: &str = "seen_message.bin";
|
pub const MESSAGE_SEEN_ID_FILE: &str = "seen_message.bin";
|
||||||
|
|
|
@ -174,6 +174,7 @@ mod generate {
|
||||||
);
|
);
|
||||||
let savefile = crate::persist::FileJson {
|
let savefile = crate::persist::FileJson {
|
||||||
version: 0,
|
version: 0,
|
||||||
|
app_id: 0,
|
||||||
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
name: crate::consts::DEFAULT_SETTINGS_NAME.to_owned(),
|
||||||
variants: mini_variants,
|
variants: mini_variants,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue