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-09-09 18:17:29 +02:00
|
|
|
use mini_moka::sync::Cache as MokaCache;
|
2024-01-22 19:26:35 +00:00
|
|
|
use mini_moka::sync::ConcurrentCacheExt;
|
2024-01-11 03:10:35 -08:00
|
|
|
|
2023-09-09 18:17:29 +02:00
|
|
|
use std::time::Duration;
|
2023-08-27 20:50:42 +03:00
|
|
|
|
2024-08-15 07:53:26 +02:00
|
|
|
use crate::{config::Config, models::aggregation_models::SearchResults};
|
2023-09-11 23:20:05 +02:00
|
|
|
|
2024-08-15 07:53:26 +02:00
|
|
|
impl From<Vec<u8>> for SearchResults {
|
|
|
|
fn from(v: Vec<u8>) -> SearchResults {
|
|
|
|
serde_json::from_slice(&v)
|
2024-08-14 21:34:18 +02:00
|
|
|
.expect("well, this can only be caused by memory corruption so good luck")
|
2024-01-11 03:10:35 -08:00
|
|
|
}
|
2023-11-28 11:47:35 +05:30
|
|
|
}
|
|
|
|
|
2024-08-15 07:53:26 +02:00
|
|
|
impl From<&SearchResults> for Vec<u8> {
|
|
|
|
fn from(v: &SearchResults) -> Vec<u8> {
|
|
|
|
serde_json::to_vec(v).expect("somehow failed to serialize search results")
|
2023-11-28 11:47:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Memory based cache backend.
|
2024-08-14 21:34:18 +02:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Cache {
|
2023-11-28 11:47:35 +05:30
|
|
|
/// The backend cache which stores data.
|
2024-01-11 03:10:35 -08:00
|
|
|
cache: MokaCache<String, Vec<u8>>,
|
2023-11-28 11:47:35 +05:30
|
|
|
}
|
|
|
|
|
2024-08-14 21:34:18 +02:00
|
|
|
impl Cache {
|
|
|
|
/// Build new cache
|
|
|
|
pub fn build(config: &Config) -> Self {
|
|
|
|
log::info!("Initializing in-memory cache");
|
2023-11-28 11:47:35 +05:30
|
|
|
|
2024-08-14 21:34:18 +02:00
|
|
|
Self {
|
2023-11-28 11:47:35 +05:30
|
|
|
cache: MokaCache::builder()
|
2024-08-14 21:34:18 +02:00
|
|
|
.time_to_live(Duration::from_secs(config.cache_expiry_time))
|
2023-11-28 11:47:35 +05:30
|
|
|
.build(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 21:34:18 +02:00
|
|
|
/// Retrieve Cached results
|
|
|
|
pub fn cached_results(&self, url: &str) -> Option<SearchResults> {
|
|
|
|
self.cache.get(&url.to_string()).map(|b| b.into())
|
2023-11-28 11:47:35 +05:30
|
|
|
}
|
|
|
|
|
2024-08-14 21:34:18 +02:00
|
|
|
/// Cache results
|
|
|
|
pub fn cache_results(&self, search_results: &[SearchResults], urls: &[String]) {
|
2024-01-22 19:26:35 +00:00
|
|
|
for (url, search_result) in urls.iter().zip(search_results.iter()) {
|
2024-08-14 21:34:18 +02:00
|
|
|
self.cache.insert(url.clone(), search_result.into());
|
2024-01-22 19:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.cache.sync();
|
2023-11-28 11:47:35 +05:30
|
|
|
}
|
|
|
|
}
|