Skip to content

Commit a8c323d

Browse files
committed
tidyup cache for byond status
1 parent 62a010e commit a8c323d

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/byond.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ pub async fn round(
5858
cache: &State<ByondTopic>,
5959
config: &State<Config>,
6060
) -> Option<Json<GameResponse>> {
61-
let mut locked = cache.cache_time.lock().unwrap();
62-
63-
if locked.is_some() {
64-
let cache_time = locked.unwrap();
65-
let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60);
66-
67-
if cache_time > five_minutes_ago {
68-
return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap()));
69-
}
61+
{
62+
match cache.cache_time.lock() {
63+
Ok(real) => {
64+
if real.is_some() {
65+
let cache_time = real.unwrap();
66+
let five_minutes_ago = chrono::Utc::now() - Duration::seconds(60);
67+
68+
if cache_time > five_minutes_ago {
69+
return Some(Json(cache.cached_status.lock().unwrap().clone().unwrap()));
70+
}
71+
}
72+
}
73+
Err(_) => {}
74+
};
7075
}
7176

7277
let topic_config_option = config.topic.clone();
@@ -113,7 +118,7 @@ pub async fn round(
113118
};
114119

115120
*cache.cached_status.lock().unwrap() = Some(byond_json.clone());
116-
*locked = Some(chrono::Utc::now());
121+
*cache.cache_time.lock().unwrap() = Some(chrono::Utc::now());
117122

118123
Some(Json(byond_json))
119124
}

0 commit comments

Comments
 (0)