diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index 920bc59..577395c 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -79,7 +79,7 @@ impl Cacher for RedisCache { "Initialising redis cache. Listening to {}", &config.redis_url ); - RedisCache::new(&config.redis_url, 5) + RedisCache::new(&config.redis_url, 5, config.cache_expiry_time) .await .expect("Redis cache configured") } @@ -113,13 +113,12 @@ pub struct InMemoryCache { #[cfg(feature = "memory-cache")] #[async_trait::async_trait] impl Cacher for InMemoryCache { - async fn build(_config: &Config) -> Self { + async fn build(config: &Config) -> Self { log::info!("Initialising in-memory cache"); InMemoryCache { cache: MokaCache::builder() - .max_capacity(1000) - .time_to_live(Duration::from_secs(60)) + .time_to_live(Duration::from_secs(config.cache_expiry_time.into())) .build(), } } diff --git a/src/cache/redis_cacher.rs b/src/cache/redis_cacher.rs index ba29841..7d3295a 100644 --- a/src/cache/redis_cacher.rs +++ b/src/cache/redis_cacher.rs @@ -18,6 +18,8 @@ pub struct RedisCache { pool_size: u8, /// It stores the index of which connection is being used at the moment. current_connection: u8, + /// It stores the max TTL for keys. + cache_ttl: u16, } impl RedisCache { @@ -36,6 +38,7 @@ impl RedisCache { pub async fn new( redis_connection_url: &str, pool_size: u8, + cache_ttl: u16, ) -> Result> { let client = Client::open(redis_connection_url)?; let mut tasks: Vec<_> = Vec::new(); @@ -48,6 +51,7 @@ impl RedisCache { connection_pool: try_join_all(tasks).await?, pool_size, current_connection: Default::default(), + cache_ttl, }; Ok(redis_cache) } @@ -121,7 +125,7 @@ impl RedisCache { let mut result: Result<(), RedisError> = self.connection_pool [self.current_connection as usize] - .set_ex(key, json_results, 600) + .set_ex(key, json_results, self.cache_ttl.into()) .await; // Code to check whether the current connection being used is dropped with connection error diff --git a/src/config/parser.rs b/src/config/parser.rs index 1cce7b0..b33936a 100644 --- a/src/config/parser.rs +++ b/src/config/parser.rs @@ -21,6 +21,9 @@ pub struct Config { /// It stores the redis connection url address on which the redis /// client should connect. pub redis_url: String, + #[cfg(any(feature = "redis-cache", feature = "memory-cache"))] + /// It stores the max TTL for search results in cache. + pub cache_expiry_time: u16, /// It stores the option to whether enable or disable production use. pub aggregator: AggregatorConfig, /// It stores the option to whether enable or disable logs. @@ -93,6 +96,19 @@ impl Config { } }; + #[cfg(any(feature = "redis-cache", feature = "memory-cache"))] + let parsed_cet = globals.get::<_, u16>("cache_expiry_time")?; + let cache_expiry_time = match parsed_cet { + 0..=59 => { + log::error!( + "Config Error: The value of `cache_expiry_time` must be greater than 60" + ); + log::error!("Falling back to using the value `60` for the option"); + 60 + } + _ => parsed_cet, + }; + Ok(Config { port: globals.get::<_, u16>("port")?, binding_ip: globals.get::<_, String>("binding_ip")?, @@ -116,6 +132,8 @@ impl Config { time_limit: rate_limiter["time_limit"], }, safe_search, + #[cfg(any(feature = "redis-cache", feature = "memory-cache"))] + cache_expiry_time, }) } } diff --git a/websurfx/config.lua b/websurfx/config.lua index eeeb638..57d552c 100644 --- a/websurfx/config.lua +++ b/websurfx/config.lua @@ -47,7 +47,7 @@ theme = "simple" -- the theme name which should be used for the website -- ### Caching ### redis_url = "redis://127.0.0.1:8082" -- redis connection url address on which the client should connect on. - +cache_expiry_time = 600 -- This option takes the expiry time of the search results (value in seconds and the value should be greater than or equal to 60 seconds). -- ### Search Engines ### upstream_search_engines = { DuckDuckGo = true,