Skip to content

Commit

Permalink
Merge branch 'rolling' into 532
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 4, 2024
2 parents 4d5e7bd + 948d20d commit c6b9340
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 64 deletions.
47 changes: 18 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "websurfx"
version = "1.17.0"
version = "1.17.20"
edition = "2021"
description = "An open-source alternative to Searx that provides clean, ad-free, and organic results with incredible speed while keeping privacy and security in mind."
repository = "https://github.com/neon-mmd/websurfx"
Expand Down Expand Up @@ -82,14 +82,13 @@ base64 = { version = "0.21.5", default-features = false, features = [
cfg-if = { version = "1.0.0", default-features = false, optional = true }
keyword_extraction = { version = "1.4.3", default-features = false, features = [
"tf_idf",


] }

stop-words = { version = "0.8.0", default-features = false, features = ["iso"] }
thesaurus = { version = "0.5.2", default-features = false, optional = true, features = [
"moby",
] }
]}
itertools = {version = "0.13.0", default-features = false}

[dev-dependencies]
rusty-hook = { version = "^0.11.2", default-features = false }
Expand Down
15 changes: 12 additions & 3 deletions src/cache/redis_cacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
use super::error::CacheError;
use error_stack::Report;
use futures::stream::FuturesUnordered;
use redis::{aio::ConnectionManager, AsyncCommands, Client, RedisError};
use redis::{
aio::ConnectionManager, AsyncCommands, Client, ExistenceCheck, RedisError, SetExpiry,
SetOptions,
};

/// A constant holding the redis pipeline size.
const REDIS_PIPELINE_SIZE: usize = 3;
Expand Down Expand Up @@ -139,8 +142,14 @@ impl RedisCache {
self.current_connection = Default::default();

for (key, json_result) in keys.zip(json_results) {
self.pipeline
.set_ex(key, json_result, self.cache_ttl.into());
self.pipeline.set_options(
key,
json_result,
SetOptions::default()
.conditional_set(ExistenceCheck::NX)
.get(true)
.with_expiration(SetExpiry::EX(self.cache_ttl.into())),
);
}

let mut result: Result<(), RedisError> = self
Expand Down
48 changes: 20 additions & 28 deletions src/server/routes/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -40,7 +41,6 @@ pub async fn search(
config: web::Data<&'static Config>,
cache: web::Data<&'static SharedCache>,
) -> Result<HttpResponse, Box<dyn std::error::Error>> {
use std::sync::Arc;
let params = web::Query::<SearchParams>::from_query(req.query_string())?;
match &params.q {
Some(query) => {
Expand Down Expand Up @@ -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<SearchResults>, Vec<String>) =
[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 = Arc::new(current_results?);
results = 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<SearchResults>, Vec<String>) =
[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 });
}
Expand Down Expand Up @@ -163,7 +155,7 @@ async fn results(
query: &str,
page: u32,
search_settings: &server_models::Cookie<'_>,
) -> Result<(SearchResults, String), Box<dyn std::error::Error>> {
) -> Result<(SearchResults, String, bool), Box<dyn std::error::Error>> {
// eagerly parse cookie value to evaluate safe search level
let safe_search_level = search_settings.safe_search_level;

Expand All @@ -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();
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit c6b9340

Please sign in to comment.