mirror of
https://git.ethanthesleepy.one/ethanaobrien/ew.git
synced 2025-05-13 11:37:33 -05:00
Generate json in a separate thread
This commit is contained in:
parent
6c791da422
commit
bb420417a7
1 changed files with 18 additions and 5 deletions
|
@ -4,6 +4,7 @@ use actix_web::{HttpResponse, HttpRequest};
|
|||
use rusqlite::{Connection, params, ToSql};
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
use lazy_static::lazy_static;
|
||||
use std::thread;
|
||||
|
||||
lazy_static! {
|
||||
static ref ENGINE: Mutex<Option<Connection>> = Mutex::new(None);
|
||||
|
@ -225,12 +226,24 @@ fn get_clearrate_json() -> JsonValue {
|
|||
result.replace(get_json());
|
||||
}
|
||||
let cache = result.as_ref().unwrap();
|
||||
if cache["last_updated"].as_u64().unwrap() + (60 * 60) > global::timestamp() {
|
||||
return cache["cache"].clone();
|
||||
let rv = cache["cache"].clone();
|
||||
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
|
||||
thread::spawn(|| {
|
||||
loop {
|
||||
match CACHED_DATA.lock() {
|
||||
Ok(mut result) => {
|
||||
let new = get_json();
|
||||
result.replace(new.clone());
|
||||
break;
|
||||
}
|
||||
Err(_) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
let new = get_json();
|
||||
result.replace(new.clone());
|
||||
return new["cache"].clone();
|
||||
return rv;
|
||||
}
|
||||
Err(_) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(15));
|
||||
|
|
Loading…
Add table
Reference in a new issue