Compare commits
1 Commits
master
...
test_selec
Author | SHA1 | Date | |
---|---|---|---|
eda704d2f1 |
24
src/main.rs
24
src/main.rs
@ -2,6 +2,7 @@
|
|||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use rocket::fs::FileServer;
|
use rocket::fs::FileServer;
|
||||||
|
use rocket::futures::FutureExt;
|
||||||
use rocket::response::Redirect;
|
use rocket::response::Redirect;
|
||||||
use rocket::State;
|
use rocket::State;
|
||||||
use rocket_dyn_templates::tera;
|
use rocket_dyn_templates::tera;
|
||||||
@ -86,10 +87,10 @@ async fn show_question_details(
|
|||||||
cache: &TemplateCache,
|
cache: &TemplateCache,
|
||||||
id: usize,
|
id: usize,
|
||||||
) -> Template {
|
) -> Template {
|
||||||
if let Some(value) = cache.get(&id) {
|
let cache_cloned = cache.clone();
|
||||||
return value.render(template_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let from_cache = rocket::tokio::spawn(async move { cache_cloned.get(&id) }).fuse();
|
||||||
|
let from_db = async move {
|
||||||
match get_question(&data.db, id).await {
|
match get_question(&data.db, id).await {
|
||||||
Ok(question) => {
|
Ok(question) => {
|
||||||
let mut context = tera::to_value(question).expect("question serialize");
|
let mut context = tera::to_value(question).expect("question serialize");
|
||||||
@ -110,6 +111,23 @@ async fn show_question_details(
|
|||||||
Template::render("404", context)
|
Template::render("404", context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
.fuse();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
rocket::tokio::select! {
|
||||||
|
biased;
|
||||||
|
|
||||||
|
Ok(Some(template)) = from_cache => {
|
||||||
|
println!("from cache");
|
||||||
|
break template.render(template_name)
|
||||||
|
},
|
||||||
|
template = from_db => {
|
||||||
|
println!("from db");
|
||||||
|
break template
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/q/<id>")]
|
#[get("/q/<id>")]
|
||||||
|
Loading…
Reference in New Issue
Block a user