File size: 2,636 Bytes
c170de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! 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.
#[derive(Clone)]
pub struct RedisCache {
    redis_connection_url: String,
}

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.
    pub fn new(redis_connection_url: String) -> Self {
        RedisCache {
            redis_connection_url,
        }
    }

    /// 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.
    fn compute_url_hash(self, url: &str) -> String {
        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.
    pub fn cached_results_json(self, url: String) -> Result<String, Box<dyn std::error::Error>> {
        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)?)
    }

    /// 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(
        self,
        json_results: String,
        url: String,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let hashed_url_string = self.clone().compute_url_hash(&url);
        let mut redis_connection: Connection =
            Client::open(self.redis_connection_url)?.get_connection()?;

        // put results_json into cache
        redis_connection.set(hashed_url_string.clone(), json_results)?;

        // Set the TTL for the key to 60 seconds
        redis_connection
            .expire::<String, u32>(hashed_url_string.clone(), 60)
            .unwrap();

        Ok(())
    }
}