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.
|
|
|
|
|
2023-08-27 13:50:42 -04:00
|
|
|
use error_stack::Report;
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
use mini_moka::sync::Cache as MokaCache;
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::sync::Mutex;
|
2023-08-27 13:50:42 -04:00
|
|
|
|
2023-09-14 14:26:08 -04:00
|
|
|
use crate::{config::parser::Config, models::aggregation_models::SearchResults};
|
2023-09-11 17:20:05 -04:00
|
|
|
|
2023-09-12 16:14:54 -04:00
|
|
|
use super::error::PoolError;
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-12 16:14:54 -04:00
|
|
|
use super::redis_cacher::RedisCache;
|
2023-05-02 04:58:21 -04:00
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
/// Different implementations for caching, currently it is possible to cache in-memory or in Redis.
|
2023-08-29 13:10:32 -04:00
|
|
|
#[derive(Clone)]
|
2023-09-09 12:17:29 -04:00
|
|
|
pub enum Cache {
|
2023-09-12 16:14:54 -04:00
|
|
|
/// Caching is disabled
|
|
|
|
Disabled,
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
/// Encapsulates the Redis based cache
|
|
|
|
Redis(RedisCache),
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
/// Contains the in-memory cache.
|
2023-09-11 17:20:05 -04:00
|
|
|
InMemory(MokaCache<String, SearchResults>),
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
impl Cache {
|
2023-09-12 16:14:54 -04:00
|
|
|
/// Builds the cache from the given configuration.
|
2023-09-14 14:26:08 -04:00
|
|
|
pub async fn build(_config: &Config) -> Self {
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-14 14:26:08 -04:00
|
|
|
if let Some(url) = &_config.redis_url {
|
2023-09-12 16:14:54 -04:00
|
|
|
log::info!("Using Redis running at {} for caching", &url);
|
|
|
|
return Cache::new(
|
|
|
|
RedisCache::new(url, 5)
|
|
|
|
.await
|
|
|
|
.expect("Redis cache configured"),
|
|
|
|
);
|
|
|
|
}
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-14 14:26:08 -04:00
|
|
|
{
|
2023-09-12 16:14:54 -04:00
|
|
|
log::info!("Using an in-memory cache");
|
|
|
|
return Cache::new_in_memory();
|
|
|
|
}
|
2023-09-14 14:26:08 -04:00
|
|
|
#[cfg(not(feature = "memory-cache"))]
|
|
|
|
{
|
|
|
|
log::info!("Caching is disabled");
|
|
|
|
Cache::Disabled
|
|
|
|
}
|
2023-09-12 16:14:54 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
/// Creates a new cache, which wraps the given RedisCache.
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
pub fn new(redis_cache: RedisCache) -> Self {
|
|
|
|
Cache::Redis(redis_cache)
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
/// Creates an in-memory cache
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
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 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
/// A function which fetches the cached json results as json string.
|
2023-05-02 04:58:21 -04:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `url` - It takes an url as a string.
|
2023-09-11 17:20:05 -04:00
|
|
|
pub async fn cached_json(&mut self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
2023-09-09 12:17:29 -04:00
|
|
|
match self {
|
2023-09-12 16:14:54 -04:00
|
|
|
Cache::Disabled => Err(Report::new(PoolError::MissingValue)),
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-11 17:20:05 -04:00
|
|
|
Cache::Redis(redis_cache) => {
|
|
|
|
let json = redis_cache.cached_json(url).await?;
|
|
|
|
Ok(serde_json::from_str::<SearchResults>(&json)
|
|
|
|
.map_err(|_| PoolError::SerializationError)?)
|
|
|
|
}
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
|
|
|
Some(res) => Ok(res),
|
|
|
|
None => Err(Report::new(PoolError::MissingValue)),
|
|
|
|
},
|
2023-08-27 13:50:42 -04:00
|
|
|
}
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
|
|
|
|
2023-09-09 12:17:29 -04: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 04:58:21 -04:00
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `json_results` - It takes the json results string as an argument.
|
|
|
|
/// * `url` - It takes the url as a String.
|
2023-08-27 13:50:42 -04:00
|
|
|
pub async fn cache_results(
|
2023-05-14 20:20:43 -04:00
|
|
|
&mut self,
|
2023-09-12 01:37:33 -04:00
|
|
|
search_results: &SearchResults,
|
2023-05-14 20:20:43 -04:00
|
|
|
url: &str,
|
2023-08-27 13:50:42 -04:00
|
|
|
) -> Result<(), Report<PoolError>> {
|
2023-09-09 12:17:29 -04:00
|
|
|
match self {
|
2023-09-12 16:14:54 -04:00
|
|
|
Cache::Disabled => Ok(()),
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "redis-cache")]
|
2023-09-11 17:20:05 -04:00
|
|
|
Cache::Redis(redis_cache) => {
|
2023-09-12 01:37:33 -04:00
|
|
|
let json = serde_json::to_string(search_results)
|
2023-09-11 17:20:05 -04:00
|
|
|
.map_err(|_| PoolError::SerializationError)?;
|
|
|
|
redis_cache.cache_results(&json, url).await
|
|
|
|
}
|
2023-09-13 17:19:44 -04:00
|
|
|
#[cfg(feature = "memory-cache")]
|
2023-09-09 12:17:29 -04:00
|
|
|
Cache::InMemory(cache) => {
|
2023-09-12 01:37:33 -04:00
|
|
|
cache.insert(url.to_string(), search_results.clone());
|
2023-09-09 12:17:29 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-02 04:58:21 -04:00
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
/// A structure to efficiently share the cache between threads - as it is protected by a Mutex.
|
|
|
|
pub struct SharedCache {
|
2023-09-14 14:26:08 -04:00
|
|
|
/// The internal cache protected from concurrent access by a mutex
|
2023-09-09 12:17:29 -04:00
|
|
|
cache: Mutex<Cache>,
|
|
|
|
}
|
2023-05-02 04:58:21 -04:00
|
|
|
|
2023-09-09 12:17:29 -04: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 13:50:42 -04:00
|
|
|
}
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|
2023-09-09 12:17:29 -04:00
|
|
|
|
2023-09-11 17:20:05 -04:00
|
|
|
/// A function which retrieves the cached SearchResulsts from the internal cache.
|
|
|
|
pub async fn cached_json(&self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
2023-09-09 12:17:29 -04:00
|
|
|
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
|
2023-09-11 17:20:05 -04:00
|
|
|
/// `SearchResults` as the value.
|
2023-09-09 12:17:29 -04:00
|
|
|
pub async fn cache_results(
|
|
|
|
&self,
|
2023-09-12 01:37:33 -04:00
|
|
|
search_results: &SearchResults,
|
2023-09-09 12:17:29 -04:00
|
|
|
url: &str,
|
|
|
|
) -> Result<(), Report<PoolError>> {
|
|
|
|
let mut mut_cache = self.cache.lock().await;
|
2023-09-11 17:20:05 -04:00
|
|
|
mut_cache.cache_results(search_results, url).await
|
2023-09-09 12:17:29 -04:00
|
|
|
}
|
2023-05-02 04:58:21 -04:00
|
|
|
}
|