From 3c7edb80d0663365c5828b95bf909af87bad857d Mon Sep 17 00:00:00 2001 From: XFFXFF <1247714429@qq.com> Date: Mon, 15 May 2023 00:20:43 +0000 Subject: [PATCH] refactor: reduce connections created with RedisCache --- .gitignore | 2 ++ src/cache/cacher.rs | 36 ++++++++++++++++-------------------- src/server/routes.rs | 12 +++++------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..c39800b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target + +dump.rdb \ No newline at end of file diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index 54d9a48..87a6c6d 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -10,9 +10,8 @@ use redis::{Client, Commands, Connection}; /// # Fields /// /// * `redis_connection_url` - It stores the redis Connection url address. -#[derive(Clone)] pub struct RedisCache { - redis_connection_url: String, + connection: Connection, } impl RedisCache { @@ -21,10 +20,11 @@ impl RedisCache { /// # Arguments /// /// * `redis_connection_url` - It stores the redis Connection url address. - pub fn new(redis_connection_url: String) -> Self { - RedisCache { - redis_connection_url, - } + pub fn new(redis_connection_url: String) -> Result> { + let client = Client::open(redis_connection_url)?; + let connection = client.get_connection()?; + let redis_cache = RedisCache { connection }; + Ok(redis_cache) } /// A helper function which computes the hash of the url and formats and returns it as string. @@ -32,7 +32,7 @@ impl RedisCache { /// # Arguments /// /// * `url` - It takes an url as string. - fn compute_url_hash(self, url: &str) -> String { + fn compute_url_hash(url: &str) -> String { format!("{:?}", compute(url)) } @@ -41,11 +41,9 @@ impl RedisCache { /// # Arguments /// /// * `url` - It takes an url as a string. - pub fn cached_results_json(self, url: String) -> Result> { - let hashed_url_string = self.clone().compute_url_hash(&url); - let mut redis_connection: Connection = - Client::open(self.redis_connection_url)?.get_connection()?; - Ok(redis_connection.get(hashed_url_string)?) + pub fn cached_results_json(&mut self, url: &str) -> Result> { + let hashed_url_string = Self::compute_url_hash(url); + Ok(self.connection.get(hashed_url_string)?) } /// A function which caches the results by using the hashed `url` as the key and @@ -57,20 +55,18 @@ impl RedisCache { /// * `json_results` - It takes the json results string as an argument. /// * `url` - It takes the url as a String. pub fn cache_results( - self, + &mut self, json_results: String, - url: String, + url: &str, ) -> Result<(), Box> { - let hashed_url_string = self.clone().compute_url_hash(&url); - let mut redis_connection: Connection = - Client::open(self.redis_connection_url)?.get_connection()?; + let hashed_url_string = Self::compute_url_hash(url); // put results_json into cache - redis_connection.set(hashed_url_string.clone(), json_results)?; + self.connection.set(&hashed_url_string, json_results)?; // Set the TTL for the key to 60 seconds - redis_connection - .expire::(hashed_url_string.clone(), 60) + self.connection + .expire::(hashed_url_string, 60) .unwrap(); Ok(()) diff --git a/src/server/routes.rs b/src/server/routes.rs index 1ee9f35..e97bc2b 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -73,7 +73,7 @@ pub async fn search( let params = web::Query::::from_query(req.query_string())?; //Initialize redis cache connection struct - let redis_cache = RedisCache::new(config.redis_connection_url.clone()); + let mut redis_cache = RedisCache::new(config.redis_connection_url.clone())?; match ¶ms.q { Some(query) => { if query.trim().is_empty() { @@ -117,7 +117,7 @@ pub async fn search( }; // fetch the cached results json. - let cached_results_json = redis_cache.clone().cached_results_json(page_url.clone()); + let cached_results_json = redis_cache.cached_results_json(&page_url); // check if fetched results was indeed fetched or it was an error and if so // handle the data accordingly. match cached_results_json { @@ -128,12 +128,10 @@ pub async fn search( } Err(_) => { let mut results_json: crate::search_results_handler::aggregation_models::SearchResults = - aggregate(query, page).await?; + aggregate(query, page).await?; results_json.add_style(config.style.clone()); - redis_cache.clone().cache_results( - serde_json::to_string(&results_json)?, - page_url.clone(), - )?; + redis_cache + .cache_results(serde_json::to_string(&results_json)?, &page_url)?; let page_content: String = hbs.render("search", &results_json)?; Ok(HttpResponse::Ok().body(page_content)) }