Set up a global maps (cache) object

This commit is contained in:
Paul van Tilburg 2022-02-12 17:20:36 +01:00
parent b5dae45868
commit d058ab4448
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
4 changed files with 10 additions and 0 deletions

1
Cargo.lock generated
View File

@ -1599,6 +1599,7 @@ dependencies = [
"chrono",
"color-eyre",
"geocoding",
"once_cell",
"rocket",
]

View File

@ -7,4 +7,5 @@ edition = "2021"
chrono = "0.4.19"
color-eyre = "0.5.6"
geocoding = "0.3.1"
once_cell = "1.9.0"
rocket = { version = "0.5.0-rc.1", features = ["json"] }

View File

@ -13,8 +13,10 @@
use color_eyre::Result;
use geocoding::{Forward, Openstreetmap, Point};
use once_cell::sync::Lazy;
use rocket::serde::json::Json;
use rocket::serde::Serialize;
use rocket::tokio::sync::Mutex;
use rocket::tokio::{self, select};
use rocket::{get, routes, FromFormField};
@ -22,6 +24,8 @@ use self::maps::Maps;
mod maps;
static MAPS: Lazy<Mutex<Maps>> = Lazy::new(|| Mutex::new(Maps::default()));
/// The current for a specific location.
///
/// Only the metrics asked for are included as well as the position and current time.

View File

@ -2,6 +2,8 @@
use rocket::tokio::time::{sleep, Duration};
use crate::MAPS;
/// The interval between map refreshes (in seconds).
const SLEEP_INTERVAL: u64 = 60;
@ -11,6 +13,8 @@ pub(crate) struct Maps;
impl Maps {
pub(crate) async fn run() -> ! {
loop {
let _maps = MAPS.lock().await;
sleep(Duration::from_secs(SLEEP_INTERVAL)).await;
}
}