mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-22 05:58:21 -05:00
Rename features, make the memory-cache the default
This commit is contained in:
parent
76795c43cc
commit
1e7805cf42
@ -67,7 +67,8 @@ rpath = false
|
|||||||
strip = "debuginfo"
|
strip = "debuginfo"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["in_memory_cache", "redis"]
|
default = ["memory-cache"]
|
||||||
dhat-heap = ["dep:dhat"]
|
dhat-heap = ["dep:dhat"]
|
||||||
in_memory_cache = ["dep:mini-moka"]
|
memory-cache = ["dep:mini-moka"]
|
||||||
redis = ["dep:redis"]
|
redis-cache = ["dep:redis"]
|
||||||
|
hybrid-cache = ["memory-cache", "redis-cache"]
|
||||||
|
25
src/cache/cacher.rs
vendored
25
src/cache/cacher.rs
vendored
@ -2,15 +2,16 @@
|
|||||||
//! from the upstream search engines in a json format.
|
//! from the upstream search engines in a json format.
|
||||||
|
|
||||||
use error_stack::Report;
|
use error_stack::Report;
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
use mini_moka::sync::Cache as MokaCache;
|
use mini_moka::sync::Cache as MokaCache;
|
||||||
|
#[cfg(feature = "memory-cache")]
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::{config::parser::Config, results::aggregation_models::SearchResults};
|
use crate::{config::parser::Config, results::aggregation_models::SearchResults};
|
||||||
|
|
||||||
use super::error::PoolError;
|
use super::error::PoolError;
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
use super::redis_cacher::RedisCache;
|
use super::redis_cacher::RedisCache;
|
||||||
|
|
||||||
/// Different implementations for caching, currently it is possible to cache in-memory or in Redis.
|
/// 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 {
|
pub enum Cache {
|
||||||
/// Caching is disabled
|
/// Caching is disabled
|
||||||
Disabled,
|
Disabled,
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
/// Encapsulates the Redis based cache
|
/// Encapsulates the Redis based cache
|
||||||
Redis(RedisCache),
|
Redis(RedisCache),
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
/// Contains the in-memory cache.
|
/// Contains the in-memory cache.
|
||||||
InMemory(MokaCache<String, SearchResults>),
|
InMemory(MokaCache<String, SearchResults>),
|
||||||
}
|
}
|
||||||
@ -29,7 +30,7 @@ pub enum Cache {
|
|||||||
impl Cache {
|
impl Cache {
|
||||||
/// Builds the cache from the given configuration.
|
/// Builds the cache from the given configuration.
|
||||||
pub async fn build(config: &Config) -> Self {
|
pub async fn build(config: &Config) -> Self {
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
if let Some(url) = &config.redis_url {
|
if let Some(url) = &config.redis_url {
|
||||||
log::info!("Using Redis running at {} for caching", &url);
|
log::info!("Using Redis running at {} for caching", &url);
|
||||||
return Cache::new(
|
return Cache::new(
|
||||||
@ -38,7 +39,7 @@ impl Cache {
|
|||||||
.expect("Redis cache configured"),
|
.expect("Redis cache configured"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
if config.in_memory_cache {
|
if config.in_memory_cache {
|
||||||
log::info!("Using an in-memory cache");
|
log::info!("Using an in-memory cache");
|
||||||
return Cache::new_in_memory();
|
return Cache::new_in_memory();
|
||||||
@ -48,13 +49,13 @@ impl Cache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new cache, which wraps the given RedisCache.
|
/// Creates a new cache, which wraps the given RedisCache.
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
pub fn new(redis_cache: RedisCache) -> Self {
|
pub fn new(redis_cache: RedisCache) -> Self {
|
||||||
Cache::Redis(redis_cache)
|
Cache::Redis(redis_cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an in-memory cache
|
/// Creates an in-memory cache
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
pub fn new_in_memory() -> Self {
|
pub fn new_in_memory() -> Self {
|
||||||
let cache = MokaCache::builder()
|
let cache = MokaCache::builder()
|
||||||
.max_capacity(1000)
|
.max_capacity(1000)
|
||||||
@ -71,13 +72,13 @@ impl Cache {
|
|||||||
pub async fn cached_json(&mut self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
pub async fn cached_json(&mut self, url: &str) -> Result<SearchResults, Report<PoolError>> {
|
||||||
match self {
|
match self {
|
||||||
Cache::Disabled => Err(Report::new(PoolError::MissingValue)),
|
Cache::Disabled => Err(Report::new(PoolError::MissingValue)),
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
Cache::Redis(redis_cache) => {
|
Cache::Redis(redis_cache) => {
|
||||||
let json = redis_cache.cached_json(url).await?;
|
let json = redis_cache.cached_json(url).await?;
|
||||||
Ok(serde_json::from_str::<SearchResults>(&json)
|
Ok(serde_json::from_str::<SearchResults>(&json)
|
||||||
.map_err(|_| PoolError::SerializationError)?)
|
.map_err(|_| PoolError::SerializationError)?)
|
||||||
}
|
}
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
Cache::InMemory(in_memory) => match in_memory.get(&url.to_string()) {
|
||||||
Some(res) => Ok(res),
|
Some(res) => Ok(res),
|
||||||
None => Err(Report::new(PoolError::MissingValue)),
|
None => Err(Report::new(PoolError::MissingValue)),
|
||||||
@ -99,13 +100,13 @@ impl Cache {
|
|||||||
) -> Result<(), Report<PoolError>> {
|
) -> Result<(), Report<PoolError>> {
|
||||||
match self {
|
match self {
|
||||||
Cache::Disabled => Ok(()),
|
Cache::Disabled => Ok(()),
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
Cache::Redis(redis_cache) => {
|
Cache::Redis(redis_cache) => {
|
||||||
let json = serde_json::to_string(search_results)
|
let json = serde_json::to_string(search_results)
|
||||||
.map_err(|_| PoolError::SerializationError)?;
|
.map_err(|_| PoolError::SerializationError)?;
|
||||||
redis_cache.cache_results(&json, url).await
|
redis_cache.cache_results(&json, url).await
|
||||||
}
|
}
|
||||||
#[cfg(feature = "in_memory_cache")]
|
#[cfg(feature = "memory-cache")]
|
||||||
Cache::InMemory(cache) => {
|
Cache::InMemory(cache) => {
|
||||||
cache.insert(url.to_string(), search_results.clone());
|
cache.insert(url.to_string(), search_results.clone());
|
||||||
Ok(())
|
Ok(())
|
||||||
|
8
src/cache/error.rs
vendored
8
src/cache/error.rs
vendored
@ -2,26 +2,28 @@
|
|||||||
//! the redis server using an async connection pool.
|
//! the redis server using an async connection pool.
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
use redis::RedisError;
|
use redis::RedisError;
|
||||||
|
|
||||||
/// A custom error type used for handling redis async pool associated errors.
|
/// A custom error type used for handling redis async pool associated errors.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum PoolError {
|
pub enum PoolError {
|
||||||
/// This variant handles all errors related to `RedisError`,
|
/// This variant handles all errors related to `RedisError`,
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
RedisError(RedisError),
|
RedisError(RedisError),
|
||||||
/// This variant handles the errors which occurs when all the connections
|
/// This variant handles the errors which occurs when all the connections
|
||||||
/// in the connection pool return a connection dropped redis error.
|
/// in the connection pool return a connection dropped redis error.
|
||||||
PoolExhaustionWithConnectionDropError,
|
PoolExhaustionWithConnectionDropError,
|
||||||
|
/// Whenever serialization or deserialization fails during communication with the cache.
|
||||||
SerializationError,
|
SerializationError,
|
||||||
|
/// Returned when the value is missing.
|
||||||
MissingValue,
|
MissingValue,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for PoolError {
|
impl fmt::Display for PoolError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
PoolError::RedisError(redis_error) => {
|
PoolError::RedisError(redis_error) => {
|
||||||
if let Some(detail) = redis_error.detail() {
|
if let Some(detail) = redis_error.detail() {
|
||||||
write!(f, "{}", detail)
|
write!(f, "{}", detail)
|
||||||
|
2
src/cache/mod.rs
vendored
2
src/cache/mod.rs
vendored
@ -3,5 +3,5 @@
|
|||||||
|
|
||||||
pub mod cacher;
|
pub mod cacher;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
#[cfg(feature = "redis")]
|
#[cfg(feature = "redis-cache")]
|
||||||
pub mod redis_cacher;
|
pub mod redis_cacher;
|
||||||
|
Loading…
Reference in New Issue
Block a user