Spaces:
Running
Running
Zsombor Gegesy
commited on
Commit
·
1e7805c
1
Parent(s):
76795c4
Rename features, make the memory-cache the default
Browse files- Cargo.toml +4 -3
- src/cache/cacher.rs +13 -12
- src/cache/error.rs +5 -3
- src/cache/mod.rs +1 -1
Cargo.toml
CHANGED
|
@@ -67,7 +67,8 @@ rpath = false
|
|
| 67 |
strip = "debuginfo"
|
| 68 |
|
| 69 |
[features]
|
| 70 |
-
default = ["
|
| 71 |
dhat-heap = ["dep:dhat"]
|
| 72 |
-
|
| 73 |
-
redis = ["dep:redis"]
|
|
|
|
|
|
| 67 |
strip = "debuginfo"
|
| 68 |
|
| 69 |
[features]
|
| 70 |
+
default = ["memory-cache"]
|
| 71 |
dhat-heap = ["dep:dhat"]
|
| 72 |
+
memory-cache = ["dep:mini-moka"]
|
| 73 |
+
redis-cache = ["dep:redis"]
|
| 74 |
+
hybrid-cache = ["memory-cache", "redis-cache"]
|
src/cache/cacher.rs
CHANGED
|
@@ -2,15 +2,16 @@
|
|
| 2 |
//! from the upstream search engines in a json format.
|
| 3 |
|
| 4 |
use error_stack::Report;
|
| 5 |
-
#[cfg(feature = "
|
| 6 |
use mini_moka::sync::Cache as MokaCache;
|
|
|
|
| 7 |
use std::time::Duration;
|
| 8 |
use tokio::sync::Mutex;
|
| 9 |
|
| 10 |
use crate::{config::parser::Config, results::aggregation_models::SearchResults};
|
| 11 |
|
| 12 |
use super::error::PoolError;
|
| 13 |
-
#[cfg(feature = "redis")]
|
| 14 |
use super::redis_cacher::RedisCache;
|
| 15 |
|
| 16 |
/// Different implementations for caching, currently it is possible to cache in-memory or in Redis.
|
|
@@ -18,10 +19,10 @@ use super::redis_cacher::RedisCache;
|
|
| 18 |
pub enum Cache {
|
| 19 |
/// Caching is disabled
|
| 20 |
Disabled,
|
| 21 |
-
#[cfg(feature = "redis")]
|
| 22 |
/// Encapsulates the Redis based cache
|
| 23 |
Redis(RedisCache),
|
| 24 |
-
#[cfg(feature = "
|
| 25 |
/// Contains the in-memory cache.
|
| 26 |
InMemory(MokaCache<String, SearchResults>),
|
| 27 |
}
|
|
@@ -29,7 +30,7 @@ pub enum Cache {
|
|
| 29 |
impl Cache {
|
| 30 |
/// Builds the cache from the given configuration.
|
| 31 |
pub async fn build(config: &Config) -> Self {
|
| 32 |
-
#[cfg(feature = "redis")]
|
| 33 |
if let Some(url) = &config.redis_url {
|
| 34 |
log::info!("Using Redis running at {} for caching", &url);
|
| 35 |
return Cache::new(
|
|
@@ -38,7 +39,7 @@ impl Cache {
|
|
| 38 |
.expect("Redis cache configured"),
|
| 39 |
);
|
| 40 |
}
|
| 41 |
-
#[cfg(feature = "
|
| 42 |
if config.in_memory_cache {
|
| 43 |
log::info!("Using an in-memory cache");
|
| 44 |
return Cache::new_in_memory();
|
|
@@ -48,13 +49,13 @@ impl Cache {
|
|
| 48 |
}
|
| 49 |
|
| 50 |
/// Creates a new cache, which wraps the given RedisCache.
|
| 51 |
-
#[cfg(feature = "redis")]
|
| 52 |
pub fn new(redis_cache: RedisCache) -> Self {
|
| 53 |
Cache::Redis(redis_cache)
|
| 54 |
}
|
| 55 |
|
| 56 |
/// Creates an in-memory cache
|
| 57 |
-
#[cfg(feature = "
|
| 58 |
pub fn new_in_memory() -> Self {
|
| 59 |
let cache = MokaCache::builder()
|
| 60 |
.max_capacity(1000)
|
|
@@ -71,13 +72,13 @@ impl Cache {
|
|
| 71 |
pub async fn cached_json(&mut self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
| 72 |
match self {
|
| 73 |
Cache::Disabled => Err(Report::new(PoolError::MissingValue)),
|
| 74 |
-
#[cfg(feature = "redis")]
|
| 75 |
Cache::Redis(redis_cache) => {
|
| 76 |
let json = redis_cache.cached_json(url).await?;
|
| 77 |
Ok(serde_json::from_str::<SearchResults>(&json)
|
| 78 |
.map_err(|_| PoolError::SerializationError)?)
|
| 79 |
}
|
| 80 |
-
#[cfg(feature = "
|
| 81 |
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
| 82 |
Some(res) => Ok(res),
|
| 83 |
None => Err(Report::new(PoolError::MissingValue)),
|
|
@@ -99,13 +100,13 @@ impl Cache {
|
|
| 99 |
) -> Result<(), Report<PoolError>> {
|
| 100 |
match self {
|
| 101 |
Cache::Disabled => Ok(()),
|
| 102 |
-
#[cfg(feature = "redis")]
|
| 103 |
Cache::Redis(redis_cache) => {
|
| 104 |
let json = serde_json::to_string(search_results)
|
| 105 |
.map_err(|_| PoolError::SerializationError)?;
|
| 106 |
redis_cache.cache_results(&json, url).await
|
| 107 |
}
|
| 108 |
-
#[cfg(feature = "
|
| 109 |
Cache::InMemory(cache) => {
|
| 110 |
cache.insert(url.to_string(), search_results.clone());
|
| 111 |
Ok(())
|
|
|
|
| 2 |
//! from the upstream search engines in a json format.
|
| 3 |
|
| 4 |
use error_stack::Report;
|
| 5 |
+
#[cfg(feature = "memory-cache")]
|
| 6 |
use mini_moka::sync::Cache as MokaCache;
|
| 7 |
+
#[cfg(feature = "memory-cache")]
|
| 8 |
use std::time::Duration;
|
| 9 |
use tokio::sync::Mutex;
|
| 10 |
|
| 11 |
use crate::{config::parser::Config, results::aggregation_models::SearchResults};
|
| 12 |
|
| 13 |
use super::error::PoolError;
|
| 14 |
+
#[cfg(feature = "redis-cache")]
|
| 15 |
use super::redis_cacher::RedisCache;
|
| 16 |
|
| 17 |
/// Different implementations for caching, currently it is possible to cache in-memory or in Redis.
|
|
|
|
| 19 |
pub enum Cache {
|
| 20 |
/// Caching is disabled
|
| 21 |
Disabled,
|
| 22 |
+
#[cfg(feature = "redis-cache")]
|
| 23 |
/// Encapsulates the Redis based cache
|
| 24 |
Redis(RedisCache),
|
| 25 |
+
#[cfg(feature = "memory-cache")]
|
| 26 |
/// Contains the in-memory cache.
|
| 27 |
InMemory(MokaCache<String, SearchResults>),
|
| 28 |
}
|
|
|
|
| 30 |
impl Cache {
|
| 31 |
/// Builds the cache from the given configuration.
|
| 32 |
pub async fn build(config: &Config) -> Self {
|
| 33 |
+
#[cfg(feature = "redis-cache")]
|
| 34 |
if let Some(url) = &config.redis_url {
|
| 35 |
log::info!("Using Redis running at {} for caching", &url);
|
| 36 |
return Cache::new(
|
|
|
|
| 39 |
.expect("Redis cache configured"),
|
| 40 |
);
|
| 41 |
}
|
| 42 |
+
#[cfg(feature = "memory-cache")]
|
| 43 |
if config.in_memory_cache {
|
| 44 |
log::info!("Using an in-memory cache");
|
| 45 |
return Cache::new_in_memory();
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
/// Creates a new cache, which wraps the given RedisCache.
|
| 52 |
+
#[cfg(feature = "redis-cache")]
|
| 53 |
pub fn new(redis_cache: RedisCache) -> Self {
|
| 54 |
Cache::Redis(redis_cache)
|
| 55 |
}
|
| 56 |
|
| 57 |
/// Creates an in-memory cache
|
| 58 |
+
#[cfg(feature = "memory-cache")]
|
| 59 |
pub fn new_in_memory() -> Self {
|
| 60 |
let cache = MokaCache::builder()
|
| 61 |
.max_capacity(1000)
|
|
|
|
| 72 |
pub async fn cached_json(&mut self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
| 73 |
match self {
|
| 74 |
Cache::Disabled => Err(Report::new(PoolError::MissingValue)),
|
| 75 |
+
#[cfg(feature = "redis-cache")]
|
| 76 |
Cache::Redis(redis_cache) => {
|
| 77 |
let json = redis_cache.cached_json(url).await?;
|
| 78 |
Ok(serde_json::from_str::<SearchResults>(&json)
|
| 79 |
.map_err(|_| PoolError::SerializationError)?)
|
| 80 |
}
|
| 81 |
+
#[cfg(feature = "memory-cache")]
|
| 82 |
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
| 83 |
Some(res) => Ok(res),
|
| 84 |
None => Err(Report::new(PoolError::MissingValue)),
|
|
|
|
| 100 |
) -> Result<(), Report<PoolError>> {
|
| 101 |
match self {
|
| 102 |
Cache::Disabled => Ok(()),
|
| 103 |
+
#[cfg(feature = "redis-cache")]
|
| 104 |
Cache::Redis(redis_cache) => {
|
| 105 |
let json = serde_json::to_string(search_results)
|
| 106 |
.map_err(|_| PoolError::SerializationError)?;
|
| 107 |
redis_cache.cache_results(&json, url).await
|
| 108 |
}
|
| 109 |
+
#[cfg(feature = "memory-cache")]
|
| 110 |
Cache::InMemory(cache) => {
|
| 111 |
cache.insert(url.to_string(), search_results.clone());
|
| 112 |
Ok(())
|
src/cache/error.rs
CHANGED
|
@@ -2,26 +2,28 @@
|
|
| 2 |
//! the redis server using an async connection pool.
|
| 3 |
use std::fmt;
|
| 4 |
|
| 5 |
-
#[cfg(feature = "redis")]
|
| 6 |
use redis::RedisError;
|
| 7 |
|
| 8 |
/// A custom error type used for handling redis async pool associated errors.
|
| 9 |
#[derive(Debug)]
|
| 10 |
pub enum PoolError {
|
| 11 |
/// This variant handles all errors related to `RedisError`,
|
| 12 |
-
#[cfg(feature = "redis")]
|
| 13 |
RedisError(RedisError),
|
| 14 |
/// This variant handles the errors which occurs when all the connections
|
| 15 |
/// in the connection pool return a connection dropped redis error.
|
| 16 |
PoolExhaustionWithConnectionDropError,
|
|
|
|
| 17 |
SerializationError,
|
|
|
|
| 18 |
MissingValue,
|
| 19 |
}
|
| 20 |
|
| 21 |
impl fmt::Display for PoolError {
|
| 22 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 23 |
match self {
|
| 24 |
-
#[cfg(feature = "redis")]
|
| 25 |
PoolError::RedisError(redis_error) => {
|
| 26 |
if let Some(detail) = redis_error.detail() {
|
| 27 |
write!(f, "{}", detail)
|
|
|
|
| 2 |
//! the redis server using an async connection pool.
|
| 3 |
use std::fmt;
|
| 4 |
|
| 5 |
+
#[cfg(feature = "redis-cache")]
|
| 6 |
use redis::RedisError;
|
| 7 |
|
| 8 |
/// A custom error type used for handling redis async pool associated errors.
|
| 9 |
#[derive(Debug)]
|
| 10 |
pub enum PoolError {
|
| 11 |
/// This variant handles all errors related to `RedisError`,
|
| 12 |
+
#[cfg(feature = "redis-cache")]
|
| 13 |
RedisError(RedisError),
|
| 14 |
/// This variant handles the errors which occurs when all the connections
|
| 15 |
/// in the connection pool return a connection dropped redis error.
|
| 16 |
PoolExhaustionWithConnectionDropError,
|
| 17 |
+
/// Whenever serialization or deserialization fails during communication with the cache.
|
| 18 |
SerializationError,
|
| 19 |
+
/// Returned when the value is missing.
|
| 20 |
MissingValue,
|
| 21 |
}
|
| 22 |
|
| 23 |
impl fmt::Display for PoolError {
|
| 24 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
| 25 |
match self {
|
| 26 |
+
#[cfg(feature = "redis-cache")]
|
| 27 |
PoolError::RedisError(redis_error) => {
|
| 28 |
if let Some(detail) = redis_error.detail() {
|
| 29 |
write!(f, "{}", detail)
|
src/cache/mod.rs
CHANGED
|
@@ -3,5 +3,5 @@
|
|
| 3 |
|
| 4 |
pub mod cacher;
|
| 5 |
pub mod error;
|
| 6 |
-
#[cfg(feature = "redis")]
|
| 7 |
pub mod redis_cacher;
|
|
|
|
| 3 |
|
| 4 |
pub mod cacher;
|
| 5 |
pub mod error;
|
| 6 |
+
#[cfg(feature = "redis-cache")]
|
| 7 |
pub mod redis_cacher;
|