From 1e7805cf42111cd2d258de580827cb8f12253462 Mon Sep 17 00:00:00 2001 From: Zsombor Gegesy Date: Wed, 13 Sep 2023 23:19:44 +0200 Subject: [PATCH] Rename features, make the memory-cache the default --- Cargo.toml | 7 ++++--- src/cache/cacher.rs | 25 +++++++++++++------------ src/cache/error.rs | 8 +++++--- src/cache/mod.rs | 2 +- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a572615..73eda46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,8 @@ rpath = false strip = "debuginfo" [features] -default = ["in_memory_cache", "redis"] +default = ["memory-cache"] dhat-heap = ["dep:dhat"] -in_memory_cache = ["dep:mini-moka"] -redis = ["dep:redis"] +memory-cache = ["dep:mini-moka"] +redis-cache = ["dep:redis"] +hybrid-cache = ["memory-cache", "redis-cache"] diff --git a/src/cache/cacher.rs b/src/cache/cacher.rs index 486ff10..a4314c9 100644 --- a/src/cache/cacher.rs +++ b/src/cache/cacher.rs @@ -2,15 +2,16 @@ //! from the upstream search engines in a json format. use error_stack::Report; -#[cfg(feature = "in_memory_cache")] +#[cfg(feature = "memory-cache")] use mini_moka::sync::Cache as MokaCache; +#[cfg(feature = "memory-cache")] use std::time::Duration; use tokio::sync::Mutex; use crate::{config::parser::Config, results::aggregation_models::SearchResults}; use super::error::PoolError; -#[cfg(feature = "redis")] +#[cfg(feature = "redis-cache")] use super::redis_cacher::RedisCache; /// Different implementations for caching, currently it is possible to cache in-memory or in Redis. @@ -18,10 +19,10 @@ use super::redis_cacher::RedisCache; pub enum Cache { /// Caching is disabled Disabled, - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] /// Encapsulates the Redis based cache Redis(RedisCache), - #[cfg(feature = "in_memory_cache")] + #[cfg(feature = "memory-cache")] /// Contains the in-memory cache. InMemory(MokaCache), } @@ -29,7 +30,7 @@ pub enum Cache { impl Cache { /// Builds the cache from the given configuration. pub async fn build(config: &Config) -> Self { - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] if let Some(url) = &config.redis_url { log::info!("Using Redis running at {} for caching", &url); return Cache::new( @@ -38,7 +39,7 @@ impl Cache { .expect("Redis cache configured"), ); } - #[cfg(feature = "in_memory_cache")] + #[cfg(feature = "memory-cache")] if config.in_memory_cache { log::info!("Using an in-memory cache"); return Cache::new_in_memory(); @@ -48,13 +49,13 @@ impl Cache { } /// Creates a new cache, which wraps the given RedisCache. - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] pub fn new(redis_cache: RedisCache) -> Self { Cache::Redis(redis_cache) } /// Creates an in-memory cache - #[cfg(feature = "in_memory_cache")] + #[cfg(feature = "memory-cache")] pub fn new_in_memory() -> Self { let cache = MokaCache::builder() .max_capacity(1000) @@ -71,13 +72,13 @@ impl Cache { pub async fn cached_json(&mut self, url: &str) -> Result> { match self { Cache::Disabled => Err(Report::new(PoolError::MissingValue)), - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] Cache::Redis(redis_cache) => { let json = redis_cache.cached_json(url).await?; Ok(serde_json::from_str::(&json) .map_err(|_| PoolError::SerializationError)?) } - #[cfg(feature = "in_memory_cache")] + #[cfg(feature = "memory-cache")] Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) { Some(res) => Ok(res), None => Err(Report::new(PoolError::MissingValue)), @@ -99,13 +100,13 @@ impl Cache { ) -> Result<(), Report> { match self { Cache::Disabled => Ok(()), - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] Cache::Redis(redis_cache) => { let json = serde_json::to_string(search_results) .map_err(|_| PoolError::SerializationError)?; redis_cache.cache_results(&json, url).await } - #[cfg(feature = "in_memory_cache")] + #[cfg(feature = "memory-cache")] Cache::InMemory(cache) => { cache.insert(url.to_string(), search_results.clone()); Ok(()) diff --git a/src/cache/error.rs b/src/cache/error.rs index 5c768a6..972511e 100644 --- a/src/cache/error.rs +++ b/src/cache/error.rs @@ -2,26 +2,28 @@ //! the redis server using an async connection pool. use std::fmt; -#[cfg(feature = "redis")] +#[cfg(feature = "redis-cache")] use redis::RedisError; /// A custom error type used for handling redis async pool associated errors. #[derive(Debug)] pub enum PoolError { /// This variant handles all errors related to `RedisError`, - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] RedisError(RedisError), /// This variant handles the errors which occurs when all the connections /// in the connection pool return a connection dropped redis error. PoolExhaustionWithConnectionDropError, + /// Whenever serialization or deserialization fails during communication with the cache. SerializationError, + /// Returned when the value is missing. MissingValue, } impl fmt::Display for PoolError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - #[cfg(feature = "redis")] + #[cfg(feature = "redis-cache")] PoolError::RedisError(redis_error) => { if let Some(detail) = redis_error.detail() { write!(f, "{}", detail) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 4c8f193..887f119 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -3,5 +3,5 @@ pub mod cacher; pub mod error; -#[cfg(feature = "redis")] +#[cfg(feature = "redis-cache")] pub mod redis_cacher;