From 9a5f1c5f449bc96d1c4c1650aba2bf23b1b22852 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Mon, 2 Sep 2024 21:10:54 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20reimplement=20caching=20c?= =?UTF-8?q?ode=20within=20the=20`search`=20function=20(#592)=20-=20reduce?= =?UTF-8?q?=20resource=20usage=20&=20-=20only=20cache=20search=20results?= =?UTF-8?q?=20which=20has=20not=20been=20cached=20before.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/routes/search.rs | 48 ++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/src/server/routes/search.rs b/src/server/routes/search.rs index 103b581..b25077c 100644 --- a/src/server/routes/search.rs +++ b/src/server/routes/search.rs @@ -12,6 +12,7 @@ use crate::{ results::aggregator::aggregate, }; use actix_web::{get, http::header::ContentType, web, HttpRequest, HttpResponse}; +use itertools::Itertools; use regex::Regex; use std::borrow::Cow; use tokio::{ @@ -40,7 +41,6 @@ pub async fn search( config: web::Data<&'static Config>, cache: web::Data<&'static SharedCache>, ) -> Result> { - use std::sync::Arc; let params = web::Query::::from_query(req.query_string())?; match ¶ms.q { Some(query) => { @@ -83,44 +83,36 @@ pub async fn search( let previous_page = page.saturating_sub(1); let next_page = page + 1; - let mut results = Arc::new((SearchResults::default(), String::default())); + let results: (SearchResults, String, bool); if page != previous_page { let (previous_results, current_results, next_results) = join!( 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 = current_results?; - results = Arc::new(current_results?); + let (results_list, cache_keys): (Vec, Vec) = + [previous_results?, results.clone(), next_results?] + .into_iter() + .filter_map(|(result, cache_key, flag)| { + dbg!(flag).then_some((result, cache_key)) + }) + .multiunzip(); 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 = current_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], - ); + let (results_list, cache_keys): (Vec, Vec) = + [results.clone(), next_results?] + .into_iter() + .filter_map(|(result, cache_key, flag)| flag.then_some((result, cache_key))) + .multiunzip(); tokio::spawn(async move { cache.cache_results(&results_list, &cache_keys).await }); } @@ -163,7 +155,7 @@ async fn results( query: &str, page: u32, search_settings: &server_models::Cookie<'_>, -) -> Result<(SearchResults, String), Box> { +) -> Result<(SearchResults, String, bool), Box> { // eagerly parse cookie value to evaluate safe search level let safe_search_level = search_settings.safe_search_level; @@ -182,7 +174,7 @@ async fn results( // check if fetched cache results was indeed fetched or it was an error and if so // handle the data accordingly. match cached_results { - Ok(results) => Ok((results, cache_key)), + Ok(results) => Ok((results, cache_key, false)), Err(_) => { if safe_search_level == 4 { let mut results: SearchResults = SearchResults::default(); @@ -196,7 +188,7 @@ async fn results( .cache_results(&[results.clone()], &[cache_key.clone()]) .await?; results.set_safe_search_level(safe_search_level); - return Ok((results, cache_key)); + return Ok((results, cache_key, true)); } } @@ -235,7 +227,7 @@ async fn results( .cache_results(&[results.clone()], &[cache_key.clone()]) .await?; results.set_safe_search_level(safe_search_level); - Ok((results, cache_key)) + Ok((results, cache_key, true)) } } }