From f5cf5f9151daa2593946689d248af233175f7476 Mon Sep 17 00:00:00 2001 From: Spencerjibz Date: Mon, 22 Jan 2024 19:26:35 +0000 Subject: [PATCH] changed cache_results method to support multiple values --- src/cache/cacher.rs | 66 ++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 22 deletions(-) diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index d1144e3..7fa881f 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -4,6 +4,7 @@ use error_stack::Report; #[cfg(feature = "memory-cache")] use mini_moka::sync::Cache as MokaCache; +use mini_moka::sync::ConcurrentCacheExt; #[cfg(feature = "memory-cache")] use std::time::Duration; @@ -61,8 +62,8 @@ pub trait Cacher: Send + Sync { /// failure. async fn cache_results( &mut self, - search_results: &SearchResults, - url: &str, + search_results: &[SearchResults], + urls: &[String], ) -> Result<(), Report>; /// A helper function which computes the hash of the url and formats and returns it as string. @@ -332,14 +333,29 @@ impl Cacher for RedisCache { async fn cache_results( &mut self, - search_results: &SearchResults, - url: &str, + search_results: &[SearchResults], + urls: &[String], ) -> Result<(), Report> { use base64::Engine; - let bytes = self.pre_process_search_results(search_results)?; - let base64_string = base64::engine::general_purpose::STANDARD_NO_PAD.encode(bytes); - let hashed_url_string = self.hash_url(url); - self.cache_json(&base64_string, &hashed_url_string).await + let mut bytes = Vec::with_capacity(3); + + for result in search_results { + let processed = self.pre_process_search_results(result)?; + bytes.push(processed); + } + + let base64_strings = bytes + .iter() + .map(|bytes_vec| base64::engine::general_purpose::STANDARD_NO_PAD.encode(bytes_vec)); + + let mut hashed_url_strings = Vec::with_capacity(3); + + for url in urls { + let hash = self.hash_url(url); + hashed_url_strings.push(hash); + } + self.cache_json(base64_strings, hashed_url_strings.into_iter()) + .await } } /// TryInto implementation for SearchResults from Vec @@ -391,12 +407,16 @@ impl Cacher for InMemoryCache { async fn cache_results( &mut self, - search_results: &SearchResults, - url: &str, + search_results: &[SearchResults], + urls: &[String], ) -> Result<(), Report> { - let hashed_url_string = self.hash_url(url); - let bytes = self.pre_process_search_results(search_results)?; - self.cache.insert(hashed_url_string, bytes); + for (url, search_result) in urls.iter().zip(search_results.iter()) { + let hashed_url_string = self.hash_url(url); + let bytes = self.pre_process_search_results(search_result)?; + self.cache.insert(hashed_url_string, bytes); + } + + self.cache.sync(); Ok(()) } } @@ -434,11 +454,13 @@ impl Cacher for HybridCache { async fn cache_results( &mut self, - search_results: &SearchResults, - url: &str, + search_results: &[SearchResults], + urls: &[String], ) -> Result<(), Report> { - self.redis_cache.cache_results(search_results, url).await?; - self.memory_cache.cache_results(search_results, url).await?; + self.redis_cache.cache_results(search_results, urls).await?; + self.memory_cache + .cache_results(search_results, urls) + .await?; Ok(()) } @@ -460,8 +482,8 @@ impl Cacher for DisabledCache { async fn cache_results( &mut self, - _search_results: &SearchResults, - _url: &str, + _search_results: &[SearchResults], + _urls: &[String], ) -> Result<(), Report> { Ok(()) } @@ -519,11 +541,11 @@ impl SharedCache { /// on a failure. pub async fn cache_results( &self, - search_results: &SearchResults, - url: &str, + search_results: &[SearchResults], + urls: &[String], ) -> Result<(), Report> { let mut mut_cache = self.cache.lock().await; - mut_cache.cache_results(search_results, url).await + mut_cache.cache_results(search_results, urls).await } }