Replace repeated Mutex lock functions with a macro

This commit is contained in:
Ethan O'Brien
2024-11-02 12:09:35 -05:00
parent d9ce65df42
commit 7aaf22c923
6 changed files with 110 additions and 167 deletions

View File

@ -1,7 +1,6 @@
use json::{object, array, JsonValue};
use rusqlite::params;
use std::sync::Mutex;
use std::thread;
use lazy_static::lazy_static;
use crate::sql::SQLite;
@ -98,36 +97,17 @@ fn get_json() -> JsonValue {
}
}
pub fn get_scores_json() -> JsonValue {
loop {
match CACHED_DATA.lock() {
Ok(mut result) => {
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
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));
}
}
}
});
}
return rv;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
pub async fn get_scores_json() -> JsonValue {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
let rv = cache["cache"].clone();
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
let new = get_json();
result.replace(new.clone());
}
return rv;
}