2023-05-02 11:58:21 +03:00
|
|
|
//! This module provides the functionality to cache the aggregated results fetched and aggregated
|
|
|
|
//! from the upstream search engines in a json format.
|
|
|
|
|
2023-08-27 20:50:42 +03:00
|
|
|
use error_stack::Report;
|
2023-09-09 18:17:29 +02:00
|
|
|
use mini_moka::sync::Cache as MokaCache;
|
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::sync::Mutex;
|
2023-08-27 20:50:42 +03:00
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
use super::{error::PoolError, redis_cacher::RedisCache};
|
2023-05-02 11:58:21 +03:00
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
/// Different implementations for caching, currently it is possible to cache in-memory or in Redis.
|
2023-08-29 20:10:32 +03:00
|
|
|
#[derive(Clone)]
|
2023-09-09 18:17:29 +02:00
|
|
|
pub enum Cache {
|
|
|
|
/// Encapsulates the Redis based cache
|
|
|
|
Redis(RedisCache),
|
|
|
|
/// Contains the in-memory cache.
|
|
|
|
InMemory(MokaCache<String, String>),
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
impl Cache {
|
|
|
|
/// Creates a new cache, which wraps the given RedisCache.
|
|
|
|
pub fn new(redis_cache: RedisCache) -> Self {
|
|
|
|
Cache::Redis(redis_cache)
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
/// Creates an in-memory cache
|
|
|
|
pub fn new_in_memory() -> Self {
|
|
|
|
let cache = MokaCache::builder()
|
|
|
|
.max_capacity(1000)
|
|
|
|
.time_to_live(Duration::from_secs(60))
|
|
|
|
.build();
|
|
|
|
Cache::InMemory(cache)
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
/// A function which fetches the cached json results as json string.
|
2023-05-02 11:58:21 +03:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `url` - It takes an url as a string.
|
2023-08-27 20:50:42 +03:00
|
|
|
pub async fn cached_json(&mut self, url: &str) -> Result<String, Report<PoolError>> {
|
2023-09-09 18:17:29 +02:00
|
|
|
match self {
|
|
|
|
Cache::Redis(redis_cache) => redis_cache.cached_json(url).await,
|
|
|
|
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
|
|
|
Some(res) => Ok(res),
|
|
|
|
None => Err(Report::new(PoolError::MissingValue)),
|
|
|
|
},
|
2023-08-27 20:50:42 +03:00
|
|
|
}
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
/// A function which caches the results by using the `url` as the key and
|
|
|
|
/// `json results` as the value and stores it in the cache
|
2023-05-02 11:58:21 +03:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `json_results` - It takes the json results string as an argument.
|
|
|
|
/// * `url` - It takes the url as a String.
|
2023-08-27 20:50:42 +03:00
|
|
|
pub async fn cache_results(
|
2023-05-15 00:20:43 +00:00
|
|
|
&mut self,
|
2023-09-09 18:17:29 +02:00
|
|
|
json_results: String,
|
2023-05-15 00:20:43 +00:00
|
|
|
url: &str,
|
2023-08-27 20:50:42 +03:00
|
|
|
) -> Result<(), Report<PoolError>> {
|
2023-09-09 18:17:29 +02:00
|
|
|
match self {
|
|
|
|
Cache::Redis(redis_cache) => redis_cache.cache_results(&json_results, url).await,
|
|
|
|
Cache::InMemory(cache) => {
|
|
|
|
cache.insert(url.to_string(), json_results);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 11:58:21 +03:00
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
/// A structure to efficiently share the cache between threads - as it is protected by a Mutex.
|
|
|
|
pub struct SharedCache {
|
|
|
|
cache: Mutex<Cache>,
|
|
|
|
}
|
2023-05-02 11:58:21 +03:00
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
impl SharedCache {
|
|
|
|
/// Creates a new SharedCache from a Cache implementation
|
|
|
|
pub fn new(cache: Cache) -> Self {
|
|
|
|
Self {
|
|
|
|
cache: Mutex::new(cache),
|
2023-08-27 20:50:42 +03:00
|
|
|
}
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|
2023-09-09 18:17:29 +02:00
|
|
|
|
|
|
|
/// A function which fetches the cached json results as json string.
|
|
|
|
pub async fn cached_json(&self, url: &str) -> Result<String, Report<PoolError>> {
|
|
|
|
let mut mut_cache = self.cache.lock().await;
|
|
|
|
mut_cache.cached_json(url).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A function which caches the results by using the `url` as the key and
|
|
|
|
/// `json results` as the value and stores it in the cache
|
|
|
|
pub async fn cache_results(
|
|
|
|
&self,
|
|
|
|
json_results: String,
|
|
|
|
url: &str,
|
|
|
|
) -> Result<(), Report<PoolError>> {
|
|
|
|
let mut mut_cache = self.cache.lock().await;
|
|
|
|
mut_cache.cache_results(json_results, url).await
|
|
|
|
}
|
2023-05-02 11:58:21 +03:00
|
|
|
}
|