refactor: reduce connections created with RedisCache
Browse files- .gitignore +2 -0
- src/cache/cacher.rs +16 -20
- src/server/routes.rs +5 -7
.gitignore
CHANGED
@@ -1 +1,3 @@
|
|
1 |
/target
|
|
|
|
|
|
1 |
/target
|
2 |
+
|
3 |
+
dump.rdb
|
src/cache/cacher.rs
CHANGED
@@ -10,9 +10,8 @@ use redis::{Client, Commands, Connection};
|
|
10 |
/// # Fields
|
11 |
///
|
12 |
/// * `redis_connection_url` - It stores the redis Connection url address.
|
13 |
-
#[derive(Clone)]
|
14 |
pub struct RedisCache {
|
15 |
-
|
16 |
}
|
17 |
|
18 |
impl RedisCache {
|
@@ -21,10 +20,11 @@ impl RedisCache {
|
|
21 |
/// # Arguments
|
22 |
///
|
23 |
/// * `redis_connection_url` - It stores the redis Connection url address.
|
24 |
-
pub fn new(redis_connection_url: String) -> Self {
|
25 |
-
|
26 |
-
|
27 |
-
}
|
|
|
28 |
}
|
29 |
|
30 |
/// A helper function which computes the hash of the url and formats and returns it as string.
|
@@ -32,7 +32,7 @@ impl RedisCache {
|
|
32 |
/// # Arguments
|
33 |
///
|
34 |
/// * `url` - It takes an url as string.
|
35 |
-
fn compute_url_hash(
|
36 |
format!("{:?}", compute(url))
|
37 |
}
|
38 |
|
@@ -41,11 +41,9 @@ impl RedisCache {
|
|
41 |
/// # Arguments
|
42 |
///
|
43 |
/// * `url` - It takes an url as a string.
|
44 |
-
pub fn cached_results_json(self, url:
|
45 |
-
let hashed_url_string =
|
46 |
-
|
47 |
-
Client::open(self.redis_connection_url)?.get_connection()?;
|
48 |
-
Ok(redis_connection.get(hashed_url_string)?)
|
49 |
}
|
50 |
|
51 |
/// A function which caches the results by using the hashed `url` as the key and
|
@@ -57,20 +55,18 @@ impl RedisCache {
|
|
57 |
/// * `json_results` - It takes the json results string as an argument.
|
58 |
/// * `url` - It takes the url as a String.
|
59 |
pub fn cache_results(
|
60 |
-
self,
|
61 |
json_results: String,
|
62 |
-
url:
|
63 |
) -> Result<(), Box<dyn std::error::Error>> {
|
64 |
-
let hashed_url_string =
|
65 |
-
let mut redis_connection: Connection =
|
66 |
-
Client::open(self.redis_connection_url)?.get_connection()?;
|
67 |
|
68 |
// put results_json into cache
|
69 |
-
|
70 |
|
71 |
// Set the TTL for the key to 60 seconds
|
72 |
-
|
73 |
-
.expire::<String, u32>(hashed_url_string
|
74 |
.unwrap();
|
75 |
|
76 |
Ok(())
|
|
|
10 |
/// # Fields
|
11 |
///
|
12 |
/// * `redis_connection_url` - It stores the redis Connection url address.
|
|
|
13 |
pub struct RedisCache {
|
14 |
+
connection: Connection,
|
15 |
}
|
16 |
|
17 |
impl RedisCache {
|
|
|
20 |
/// # Arguments
|
21 |
///
|
22 |
/// * `redis_connection_url` - It stores the redis Connection url address.
|
23 |
+
pub fn new(redis_connection_url: String) -> Result<Self, Box<dyn std::error::Error>> {
|
24 |
+
let client = Client::open(redis_connection_url)?;
|
25 |
+
let connection = client.get_connection()?;
|
26 |
+
let redis_cache = RedisCache { connection };
|
27 |
+
Ok(redis_cache)
|
28 |
}
|
29 |
|
30 |
/// A helper function which computes the hash of the url and formats and returns it as string.
|
|
|
32 |
/// # Arguments
|
33 |
///
|
34 |
/// * `url` - It takes an url as string.
|
35 |
+
fn compute_url_hash(url: &str) -> String {
|
36 |
format!("{:?}", compute(url))
|
37 |
}
|
38 |
|
|
|
41 |
/// # Arguments
|
42 |
///
|
43 |
/// * `url` - It takes an url as a string.
|
44 |
+
pub fn cached_results_json(&mut self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
|
45 |
+
let hashed_url_string = Self::compute_url_hash(url);
|
46 |
+
Ok(self.connection.get(hashed_url_string)?)
|
|
|
|
|
47 |
}
|
48 |
|
49 |
/// A function which caches the results by using the hashed `url` as the key and
|
|
|
55 |
/// * `json_results` - It takes the json results string as an argument.
|
56 |
/// * `url` - It takes the url as a String.
|
57 |
pub fn cache_results(
|
58 |
+
&mut self,
|
59 |
json_results: String,
|
60 |
+
url: &str,
|
61 |
) -> Result<(), Box<dyn std::error::Error>> {
|
62 |
+
let hashed_url_string = Self::compute_url_hash(url);
|
|
|
|
|
63 |
|
64 |
// put results_json into cache
|
65 |
+
self.connection.set(&hashed_url_string, json_results)?;
|
66 |
|
67 |
// Set the TTL for the key to 60 seconds
|
68 |
+
self.connection
|
69 |
+
.expire::<String, u32>(hashed_url_string, 60)
|
70 |
.unwrap();
|
71 |
|
72 |
Ok(())
|
src/server/routes.rs
CHANGED
@@ -73,7 +73,7 @@ pub async fn search(
|
|
73 |
let params = web::Query::<SearchParams>::from_query(req.query_string())?;
|
74 |
|
75 |
//Initialize redis cache connection struct
|
76 |
-
let redis_cache = RedisCache::new(config.redis_connection_url.clone())
|
77 |
match ¶ms.q {
|
78 |
Some(query) => {
|
79 |
if query.trim().is_empty() {
|
@@ -117,7 +117,7 @@ pub async fn search(
|
|
117 |
};
|
118 |
|
119 |
// fetch the cached results json.
|
120 |
-
let cached_results_json = redis_cache.
|
121 |
// check if fetched results was indeed fetched or it was an error and if so
|
122 |
// handle the data accordingly.
|
123 |
match cached_results_json {
|
@@ -128,12 +128,10 @@ pub async fn search(
|
|
128 |
}
|
129 |
Err(_) => {
|
130 |
let mut results_json: crate::search_results_handler::aggregation_models::SearchResults =
|
131 |
-
|
132 |
results_json.add_style(config.style.clone());
|
133 |
-
redis_cache
|
134 |
-
serde_json::to_string(&results_json)?,
|
135 |
-
page_url.clone(),
|
136 |
-
)?;
|
137 |
let page_content: String = hbs.render("search", &results_json)?;
|
138 |
Ok(HttpResponse::Ok().body(page_content))
|
139 |
}
|
|
|
73 |
let params = web::Query::<SearchParams>::from_query(req.query_string())?;
|
74 |
|
75 |
//Initialize redis cache connection struct
|
76 |
+
let mut redis_cache = RedisCache::new(config.redis_connection_url.clone())?;
|
77 |
match ¶ms.q {
|
78 |
Some(query) => {
|
79 |
if query.trim().is_empty() {
|
|
|
117 |
};
|
118 |
|
119 |
// fetch the cached results json.
|
120 |
+
let cached_results_json = redis_cache.cached_results_json(&page_url);
|
121 |
// check if fetched results was indeed fetched or it was an error and if so
|
122 |
// handle the data accordingly.
|
123 |
match cached_results_json {
|
|
|
128 |
}
|
129 |
Err(_) => {
|
130 |
let mut results_json: crate::search_results_handler::aggregation_models::SearchResults =
|
131 |
+
aggregate(query, page).await?;
|
132 |
results_json.add_style(config.style.clone());
|
133 |
+
redis_cache
|
134 |
+
.cache_results(serde_json::to_string(&results_json)?, &page_url)?;
|
|
|
|
|
135 |
let page_content: String = hbs.render("search", &results_json)?;
|
136 |
Ok(HttpResponse::Ok().body(page_content))
|
137 |
}
|