test concurent get q from db/cache
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
da962a9f7a
commit
eda704d2f1
56
src/main.rs
56
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,28 +87,45 @@ 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
match get_question(&data.db, id).await {
|
let from_cache = rocket::tokio::spawn(async move { cache_cloned.get(&id) }).fuse();
|
||||||
Ok(question) => {
|
let from_db = async move {
|
||||||
let mut context = tera::to_value(question).expect("question serialize");
|
match get_question(&data.db, id).await {
|
||||||
if context.is_object() {
|
Ok(question) => {
|
||||||
let next_id = random_question_id(&data.database_distribution);
|
let mut context = tera::to_value(question).expect("question serialize");
|
||||||
context["next"] = tera::to_value(next_id).expect("question id serialize");
|
if context.is_object() {
|
||||||
|
let next_id = random_question_id(&data.database_distribution);
|
||||||
|
context["next"] = tera::to_value(next_id).expect("question id serialize");
|
||||||
|
}
|
||||||
|
|
||||||
|
let value = ArcTemplateData::new(context);
|
||||||
|
let result = value.render(template_name);
|
||||||
|
cache.insert(id, value);
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
use std::collections::HashMap;
|
||||||
|
let context: HashMap<String, String> = HashMap::new();
|
||||||
|
Template::render("404", context)
|
||||||
}
|
}
|
||||||
|
|
||||||
let value = ArcTemplateData::new(context);
|
|
||||||
let result = value.render(template_name);
|
|
||||||
cache.insert(id, value);
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
Err(_) => {
|
}
|
||||||
use std::collections::HashMap;
|
.fuse();
|
||||||
let context: HashMap<String, String> = HashMap::new();
|
|
||||||
Template::render("404", context)
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user