2023-05-02 04:58:21 -04:00
|
|
|
//! This module provides the functionality to cache the aggregated results fetched and aggregated
|
|
|
|
//! from the upstream search engines in a json format.
|
|
|
|
|
|
|
|
use md5::compute;
|
|
|
|
use redis::{Client, Commands, Connection};
|
|
|
|
|
|
|
|
/// A named struct which stores the redis Connection url address to which the client will
|
|
|
|
/// connect to.
|
|
|
|
///
|
|
|
|
/// # Fields
|
|
|
|
///
|
|
|
|
/// * `redis_connection_url` - It stores the redis Connection url address.
|
|
|
|
pub struct RedisCache {
|
2023-05-14 20:20:43 -04:00
|
|
|
connection: Connection,
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedisCache {
|
|
|
|
/// Constructs a new `SearchResult` with the given arguments needed for the struct.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `redis_connection_url` - It stores the redis Connection url address.
|
2023-05-14 20:20:43 -04:00
|
|
|
pub fn new(redis_connection_url: String) -> Result<Self, Box<dyn std::error::Error>> {
|
|
|
|
let client = Client::open(redis_connection_url)?;
|
|
|
|
let connection = client.get_connection()?;
|
|
|
|
let redis_cache = RedisCache { connection };
|
|
|
|
Ok(redis_cache)
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A helper function which computes the hash of the url and formats and returns it as string.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `url` - It takes an url as string.
|
2023-07-03 13:30:25 -04:00
|
|
|
fn hash_url(url: &str) -> String {
|
2023-05-02 04:58:21 -04:00
|
|
|
format!("{:?}", compute(url))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A function which fetches the cached json results as json string from the redis server.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `url` - It takes an url as a string.
|
2023-07-03 13:30:25 -04:00
|
|
|
pub fn get_cached_json(&mut self, url: &str) -> Result<String, Box<dyn std::error::Error>> {
|
|
|
|
let hashed_url_string = Self::hash_url(url);
|
2023-05-14 20:20:43 -04:00
|
|
|
Ok(self.connection.get(hashed_url_string)?)
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A function which caches the results by using the hashed `url` as the key and
|
|
|
|
/// `json results` as the value and stores it in redis server with ttl(time to live)
|
|
|
|
/// set to 60 seconds.
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `json_results` - It takes the json results string as an argument.
|
|
|
|
/// * `url` - It takes the url as a String.
|
|
|
|
pub fn cache_results(
|
2023-05-14 20:20:43 -04:00
|
|
|
&mut self,
|
2023-05-02 04:58:21 -04:00
|
|
|
json_results: String,
|
2023-05-14 20:20:43 -04:00
|
|
|
url: &str,
|
2023-05-02 04:58:21 -04:00
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
2023-07-03 13:30:25 -04:00
|
|
|
let hashed_url_string = Self::hash_url(url);
|
2023-05-02 04:58:21 -04:00
|
|
|
|
|
|
|
// put results_json into cache
|
2023-05-14 20:20:43 -04:00
|
|
|
self.connection.set(&hashed_url_string, json_results)?;
|
2023-05-02 04:58:21 -04:00
|
|
|
|
|
|
|
// Set the TTL for the key to 60 seconds
|
2023-05-14 20:20:43 -04:00
|
|
|
self.connection
|
|
|
|
.expire::<String, u32>(hashed_url_string, 60)
|
2023-05-02 04:58:21 -04:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|