Skip to content

Commit

Permalink
tidyup cache for byond status
Browse files Browse the repository at this point in the history
  • Loading branch information
harryob committed Nov 4, 2024
1 parent 62a010e commit a8c323d
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/byond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,20 @@ pub async fn round(
cache: &State<ByondTopic>,
config: &State<Config>,
) -> Option<Json<GameResponse>> {
let mut locked = cache.cache_time.lock().unwrap();

if locked.is_some() {
let cache_time = locked.unwrap();
let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60);

if cache_time > five_minutes_ago {
return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap()));
}
{
match cache.cache_time.lock() {
Ok(real) => {
if real.is_some() {
let cache_time = real.unwrap();
let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60);

if cache_time > five_minutes_ago {
return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap()));
}
}
}
Err(_) => {}
};
}

let topic_config_option = config.topic.clone();
Expand Down Expand Up @@ -113,7 +118,7 @@ pub async fn round(
};

*cache.cached_status.lock().unwrap() = Some(byond_json.clone());
*locked = Some(chrono::Utc::now());
*cache.cache_time.lock().unwrap() = Some(chrono::Utc::now());

Some(Json(byond_json))
}
Expand Down

0 comments on commit a8c323d

Please sign in to comment.