0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-24 06:58:22 -05:00

cache the next, current and previous results in a separate task

This commit is contained in:
Spencerjibz 2024-01-22 19:29:08 +00:00
parent f5cf5f9151
commit 6b9469e4b3

View File

@ -40,6 +40,7 @@ pub async fn search(
config: web::Data<Config>, config: web::Data<Config>,
cache: web::Data<SharedCache>, cache: web::Data<SharedCache>,
) -> Result<HttpResponse, Box<dyn std::error::Error>> { ) -> Result<HttpResponse, Box<dyn std::error::Error>> {
use std::sync::Arc;
let params = web::Query::<SearchParams>::from_query(req.query_string())?; let params = web::Query::<SearchParams>::from_query(req.query_string())?;
match &params.q { match &params.q {
Some(query) => { Some(query) => {
@ -79,12 +80,50 @@ pub async fn search(
// .max(1) makes sure that the page >= 0. // .max(1) makes sure that the page >= 0.
let page = params.page.unwrap_or(1).max(1) - 1; let page = params.page.unwrap_or(1).max(1) - 1;
let previous_page = page.saturating_sub(1);
let next_page = page + 1;
let (_, results, _) = join!( let mut results = Arc::new((SearchResults::default(), String::default()));
get_results(page.saturating_sub(1)), if page != previous_page {
get_results(page), let (previous_results, current_results, next_results) = join!(
get_results(page + 1) get_results(previous_page),
); get_results(page),
get_results(next_page)
);
let (parsed_previous_results, parsed_next_results) =
(previous_results?, next_results?);
let (cache_keys, results_list) = (
[
parsed_previous_results.1,
results.1.clone(),
parsed_next_results.1,
],
[
parsed_previous_results.0,
results.0.clone(),
parsed_next_results.0,
],
);
results = Arc::new(current_results?);
tokio::spawn(async move { cache.cache_results(&results_list, &cache_keys).await });
} else {
let (current_results, next_results) =
join!(get_results(page), get_results(page + 1));
let parsed_next_results = next_results?;
results = Arc::new(current_results?);
let (cache_keys, results_list) = (
[results.1.clone(), parsed_next_results.1.clone()],
[results.0.clone(), parsed_next_results.0],
);
tokio::spawn(async move { cache.cache_results(&results_list, &cache_keys).await });
}
Ok(HttpResponse::Ok().content_type(ContentType::html()).body( Ok(HttpResponse::Ok().content_type(ContentType::html()).body(
crate::templates::views::search::search( crate::templates::views::search::search(
@ -92,7 +131,7 @@ pub async fn search(
&config.style.theme, &config.style.theme,
&config.style.animation, &config.style.animation,
query, query,
&results?, &results.0,
) )
.0, .0,
)) ))
@ -124,7 +163,7 @@ async fn results(
query: &str, query: &str,
page: u32, page: u32,
search_settings: &server_models::Cookie<'_>, search_settings: &server_models::Cookie<'_>,
) -> Result<SearchResults, Box<dyn std::error::Error>> { ) -> Result<(SearchResults, String), Box<dyn std::error::Error>> {
// eagerly parse cookie value to evaluate safe search level // eagerly parse cookie value to evaluate safe search level
let safe_search_level = search_settings.safe_search_level; let safe_search_level = search_settings.safe_search_level;
@ -143,7 +182,7 @@ async fn results(
// check if fetched cache results was indeed fetched or it was an error and if so // check if fetched cache results was indeed fetched or it was an error and if so
// handle the data accordingly. // handle the data accordingly.
match cached_results { match cached_results {
Ok(results) => Ok(results), Ok(results) => Ok((results, cache_key)),
Err(_) => { Err(_) => {
if safe_search_level == 4 { if safe_search_level == 4 {
let mut results: SearchResults = SearchResults::default(); let mut results: SearchResults = SearchResults::default();
@ -153,9 +192,11 @@ async fn results(
// Return early when query contains disallowed words, // Return early when query contains disallowed words,
if flag { if flag {
results.set_disallowed(); results.set_disallowed();
cache.cache_results(&results, &cache_key).await?; cache
.cache_results(&[results.clone()], &[cache_key.clone()])
.await?;
results.set_safe_search_level(safe_search_level); results.set_safe_search_level(safe_search_level);
return Ok(results); return Ok((results, cache_key));
} }
} }
@ -173,7 +214,7 @@ async fn results(
&search_settings &search_settings
.engines .engines
.iter() .iter()
.filter_map(|engine| EngineHandler::new(&engine).ok()) .filter_map(|engine| EngineHandler::new(engine).ok())
.collect::<Vec<EngineHandler>>(), .collect::<Vec<EngineHandler>>(),
config.request_timeout, config.request_timeout,
safe_search_level, safe_search_level,
@ -192,9 +233,11 @@ async fn results(
{ {
results.set_filtered(); results.set_filtered();
} }
cache.cache_results(&results, &cache_key).await?; cache
.cache_results(&[results.clone()], &[cache_key.clone()])
.await?;
results.set_safe_search_level(safe_search_level); results.set_safe_search_level(safe_search_level);
Ok(results) Ok((results, cache_key))
} }
} }
} }