I am trying to cache an HTTP response of raw JSON data. The following seems to work but having to call &Vec<T>.to_vec()
on every response seems inefficient if the data is copied each time. Is there a way to avoid this? Perhaps maybe using a &[u8]
instead of a Vec<u8>
?
use std::collections::HashMap;
use std::fs;
use rocket::response::content::RawJson;
use rocket::State;
#[rocket::get("/")]
fn coming_up(cache: &State<HashMap<&str, Vec<u8>>>) -> RawJson<Vec<u8>> {
RawJson(cache.get("coming_up").unwrap().to_vec())
}
#[rocket::launch]
fn run() -> _ {
let mut cache = HashMap::new();
let fixtures = fs::read("sample_fixtures.json").unwrap();
cache.insert("coming_up", fixtures);
rocket::build()
.manage(cache)
.mount("/api/v2/fixtures", rocket::routes![coming_up])
}