Spaces:
Runtime error
Runtime error
neon_arch
commited on
Commit
•
d33129c
1
Parent(s):
1726ccc
🧹 chore: make clippy happy (#244)
Browse files- src/cache/cacher.rs +29 -27
- src/cache/error.rs +7 -7
- src/cache/redis_cacher.rs +7 -7
- tests/index.rs +1 -0
src/cache/cacher.rs
CHANGED
@@ -10,7 +10,7 @@ use tokio::sync::Mutex;
|
|
10 |
|
11 |
use crate::{config::parser::Config, models::aggregation_models::SearchResults};
|
12 |
|
13 |
-
use super::error::
|
14 |
#[cfg(feature = "redis-cache")]
|
15 |
use super::redis_cacher::RedisCache;
|
16 |
|
@@ -42,25 +42,27 @@ impl Cache {
|
|
42 |
/// It returns a newly initialized variant based on the feature enabled by the user.
|
43 |
pub async fn build(_config: &Config) -> Self {
|
44 |
#[cfg(all(feature = "redis-cache", feature = "memory-cache"))]
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
log::info!("Listening redis server on {}", &_config.redis_url);
|
54 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
.
|
59 |
-
|
|
|
|
|
|
|
60 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
61 |
{
|
62 |
log::info!("Using an in-memory cache");
|
63 |
-
|
64 |
}
|
65 |
#[cfg(not(any(feature = "memory-cache", feature = "redis-cache")))]
|
66 |
{
|
@@ -131,27 +133,27 @@ impl Cache {
|
|
131 |
///
|
132 |
/// Returns the `SearchResults` from the cache if the program executes normally otherwise
|
133 |
/// returns a `CacheError` if the results cannot be retrieved from the cache.
|
134 |
-
pub async fn cached_json(&mut self, _url: &str) -> Result<SearchResults, Report<
|
135 |
match self {
|
136 |
-
Cache::Disabled => Err(Report::new(
|
137 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
138 |
Cache::Redis(redis_cache) => {
|
139 |
let json = redis_cache.cached_json(_url).await?;
|
140 |
Ok(serde_json::from_str::<SearchResults>(&json)
|
141 |
-
.map_err(|_|
|
142 |
}
|
143 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
144 |
Cache::InMemory(in_memory) => match in_memory.get(&_url.to_string()) {
|
145 |
Some(res) => Ok(res),
|
146 |
-
None => Err(Report::new(
|
147 |
},
|
148 |
#[cfg(all(feature = "redis-cache", feature = "memory-cache"))]
|
149 |
Cache::Hybrid(redis_cache, in_memory) => match redis_cache.cached_json(_url).await {
|
150 |
Ok(res) => Ok(serde_json::from_str::<SearchResults>(&res)
|
151 |
-
.map_err(|_|
|
152 |
Err(_) => match in_memory.get(&_url.to_string()) {
|
153 |
Some(res) => Ok(res),
|
154 |
-
None => Err(Report::new(
|
155 |
},
|
156 |
},
|
157 |
}
|
@@ -174,13 +176,13 @@ impl Cache {
|
|
174 |
&mut self,
|
175 |
_search_results: &SearchResults,
|
176 |
_url: &str,
|
177 |
-
) -> Result<(), Report<
|
178 |
match self {
|
179 |
Cache::Disabled => Ok(()),
|
180 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
181 |
Cache::Redis(redis_cache) => {
|
182 |
let json = serde_json::to_string(_search_results)
|
183 |
-
.map_err(|_|
|
184 |
redis_cache.cache_results(&json, _url).await
|
185 |
}
|
186 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
@@ -191,7 +193,7 @@ impl Cache {
|
|
191 |
#[cfg(all(feature = "memory-cache", feature = "redis-cache"))]
|
192 |
Cache::Hybrid(redis_cache, cache) => {
|
193 |
let json = serde_json::to_string(_search_results)
|
194 |
-
.map_err(|_|
|
195 |
match redis_cache.cache_results(&json, _url).await {
|
196 |
Ok(_) => Ok(()),
|
197 |
Err(_) => {
|
@@ -235,7 +237,7 @@ impl SharedCache {
|
|
235 |
///
|
236 |
/// Returns a `SearchResults` struct containing the search results from the cache if nothing
|
237 |
/// goes wrong otherwise returns a `CacheError`.
|
238 |
-
pub async fn cached_json(&self, url: &str) -> Result<SearchResults, Report<
|
239 |
let mut mut_cache = self.cache.lock().await;
|
240 |
mut_cache.cached_json(url).await
|
241 |
}
|
@@ -258,7 +260,7 @@ impl SharedCache {
|
|
258 |
&self,
|
259 |
search_results: &SearchResults,
|
260 |
url: &str,
|
261 |
-
) -> Result<(), Report<
|
262 |
let mut mut_cache = self.cache.lock().await;
|
263 |
mut_cache.cache_results(search_results, url).await
|
264 |
}
|
|
|
10 |
|
11 |
use crate::{config::parser::Config, models::aggregation_models::SearchResults};
|
12 |
|
13 |
+
use super::error::CacheError;
|
14 |
#[cfg(feature = "redis-cache")]
|
15 |
use super::redis_cacher::RedisCache;
|
16 |
|
|
|
42 |
/// It returns a newly initialized variant based on the feature enabled by the user.
|
43 |
pub async fn build(_config: &Config) -> Self {
|
44 |
#[cfg(all(feature = "redis-cache", feature = "memory-cache"))]
|
45 |
+
{
|
46 |
+
log::info!("Using a hybrid cache");
|
47 |
+
Cache::new_hybrid(
|
48 |
+
RedisCache::new(&_config.redis_url, 5)
|
49 |
+
.await
|
50 |
+
.expect("Redis cache configured"),
|
51 |
+
)
|
52 |
+
}
|
|
|
53 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
54 |
+
{
|
55 |
+
log::info!("Listening redis server on {}", &_config.redis_url);
|
56 |
+
Cache::new(
|
57 |
+
RedisCache::new(&_config.redis_url, 5)
|
58 |
+
.await
|
59 |
+
.expect("Redis cache configured"),
|
60 |
+
)
|
61 |
+
}
|
62 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
63 |
{
|
64 |
log::info!("Using an in-memory cache");
|
65 |
+
Cache::new_in_memory()
|
66 |
}
|
67 |
#[cfg(not(any(feature = "memory-cache", feature = "redis-cache")))]
|
68 |
{
|
|
|
133 |
///
|
134 |
/// Returns the `SearchResults` from the cache if the program executes normally otherwise
|
135 |
/// returns a `CacheError` if the results cannot be retrieved from the cache.
|
136 |
+
pub async fn cached_json(&mut self, _url: &str) -> Result<SearchResults, Report<CacheError>> {
|
137 |
match self {
|
138 |
+
Cache::Disabled => Err(Report::new(CacheError::MissingValue)),
|
139 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
140 |
Cache::Redis(redis_cache) => {
|
141 |
let json = redis_cache.cached_json(_url).await?;
|
142 |
Ok(serde_json::from_str::<SearchResults>(&json)
|
143 |
+
.map_err(|_| CacheError::SerializationError)?)
|
144 |
}
|
145 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
146 |
Cache::InMemory(in_memory) => match in_memory.get(&_url.to_string()) {
|
147 |
Some(res) => Ok(res),
|
148 |
+
None => Err(Report::new(CacheError::MissingValue)),
|
149 |
},
|
150 |
#[cfg(all(feature = "redis-cache", feature = "memory-cache"))]
|
151 |
Cache::Hybrid(redis_cache, in_memory) => match redis_cache.cached_json(_url).await {
|
152 |
Ok(res) => Ok(serde_json::from_str::<SearchResults>(&res)
|
153 |
+
.map_err(|_| CacheError::SerializationError)?),
|
154 |
Err(_) => match in_memory.get(&_url.to_string()) {
|
155 |
Some(res) => Ok(res),
|
156 |
+
None => Err(Report::new(CacheError::MissingValue)),
|
157 |
},
|
158 |
},
|
159 |
}
|
|
|
176 |
&mut self,
|
177 |
_search_results: &SearchResults,
|
178 |
_url: &str,
|
179 |
+
) -> Result<(), Report<CacheError>> {
|
180 |
match self {
|
181 |
Cache::Disabled => Ok(()),
|
182 |
#[cfg(all(feature = "redis-cache", not(feature = "memory-cache")))]
|
183 |
Cache::Redis(redis_cache) => {
|
184 |
let json = serde_json::to_string(_search_results)
|
185 |
+
.map_err(|_| CacheError::SerializationError)?;
|
186 |
redis_cache.cache_results(&json, _url).await
|
187 |
}
|
188 |
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
|
|
193 |
#[cfg(all(feature = "memory-cache", feature = "redis-cache"))]
|
194 |
Cache::Hybrid(redis_cache, cache) => {
|
195 |
let json = serde_json::to_string(_search_results)
|
196 |
+
.map_err(|_| CacheError::SerializationError)?;
|
197 |
match redis_cache.cache_results(&json, _url).await {
|
198 |
Ok(_) => Ok(()),
|
199 |
Err(_) => {
|
|
|
237 |
///
|
238 |
/// Returns a `SearchResults` struct containing the search results from the cache if nothing
|
239 |
/// goes wrong otherwise returns a `CacheError`.
|
240 |
+
pub async fn cached_json(&self, url: &str) -> Result<SearchResults, Report<CacheError>> {
|
241 |
let mut mut_cache = self.cache.lock().await;
|
242 |
mut_cache.cached_json(url).await
|
243 |
}
|
|
|
260 |
&self,
|
261 |
search_results: &SearchResults,
|
262 |
url: &str,
|
263 |
+
) -> Result<(), Report<CacheError>> {
|
264 |
let mut mut_cache = self.cache.lock().await;
|
265 |
mut_cache.cache_results(search_results, url).await
|
266 |
}
|
src/cache/error.rs
CHANGED
@@ -7,7 +7,7 @@ use redis::RedisError;
|
|
7 |
|
8 |
/// A custom error type used for handling redis async pool associated errors.
|
9 |
#[derive(Debug)]
|
10 |
-
pub enum
|
11 |
/// This variant handles all errors related to `RedisError`,
|
12 |
#[cfg(feature = "redis-cache")]
|
13 |
RedisError(RedisError),
|
@@ -20,31 +20,31 @@ pub enum PoolError {
|
|
20 |
MissingValue,
|
21 |
}
|
22 |
|
23 |
-
impl fmt::Display for
|
24 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
25 |
match self {
|
26 |
#[cfg(feature = "redis-cache")]
|
27 |
-
|
28 |
if let Some(detail) = redis_error.detail() {
|
29 |
write!(f, "{}", detail)
|
30 |
} else {
|
31 |
write!(f, "")
|
32 |
}
|
33 |
}
|
34 |
-
|
35 |
write!(
|
36 |
f,
|
37 |
"Error all connections from the pool dropped with connection error"
|
38 |
)
|
39 |
}
|
40 |
-
|
41 |
write!(f, "The value is missing from the cache")
|
42 |
}
|
43 |
-
|
44 |
write!(f, "Unable to serialize, deserialize from the cache")
|
45 |
}
|
46 |
}
|
47 |
}
|
48 |
}
|
49 |
|
50 |
-
impl error_stack::Context for
|
|
|
7 |
|
8 |
/// A custom error type used for handling redis async pool associated errors.
|
9 |
#[derive(Debug)]
|
10 |
+
pub enum CacheError {
|
11 |
/// This variant handles all errors related to `RedisError`,
|
12 |
#[cfg(feature = "redis-cache")]
|
13 |
RedisError(RedisError),
|
|
|
20 |
MissingValue,
|
21 |
}
|
22 |
|
23 |
+
impl fmt::Display for CacheError {
|
24 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
25 |
match self {
|
26 |
#[cfg(feature = "redis-cache")]
|
27 |
+
CacheError::RedisError(redis_error) => {
|
28 |
if let Some(detail) = redis_error.detail() {
|
29 |
write!(f, "{}", detail)
|
30 |
} else {
|
31 |
write!(f, "")
|
32 |
}
|
33 |
}
|
34 |
+
CacheError::PoolExhaustionWithConnectionDropError => {
|
35 |
write!(
|
36 |
f,
|
37 |
"Error all connections from the pool dropped with connection error"
|
38 |
)
|
39 |
}
|
40 |
+
CacheError::MissingValue => {
|
41 |
write!(f, "The value is missing from the cache")
|
42 |
}
|
43 |
+
CacheError::SerializationError => {
|
44 |
write!(f, "Unable to serialize, deserialize from the cache")
|
45 |
}
|
46 |
}
|
47 |
}
|
48 |
}
|
49 |
|
50 |
+
impl error_stack::Context for CacheError {}
|
src/cache/redis_cacher.rs
CHANGED
@@ -6,7 +6,7 @@ use futures::future::try_join_all;
|
|
6 |
use md5::compute;
|
7 |
use redis::{aio::ConnectionManager, AsyncCommands, Client, RedisError};
|
8 |
|
9 |
-
use super::error::
|
10 |
|
11 |
/// A named struct which stores the redis Connection url address to which the client will
|
12 |
/// connect to.
|
@@ -72,7 +72,7 @@ impl RedisCache {
|
|
72 |
///
|
73 |
/// Returns the results as a String from the cache on success otherwise returns a `CacheError`
|
74 |
/// on a failure.
|
75 |
-
pub async fn cached_json(&mut self, url: &str) -> Result<String, Report<
|
76 |
self.current_connection = Default::default();
|
77 |
let hashed_url_string: &str = &self.hash_url(url);
|
78 |
|
@@ -95,7 +95,7 @@ impl RedisCache {
|
|
95 |
self.current_connection += 1;
|
96 |
if self.current_connection == self.pool_size {
|
97 |
return Err(Report::new(
|
98 |
-
|
99 |
));
|
100 |
}
|
101 |
result = self.connection_pool[self.current_connection as usize]
|
@@ -103,7 +103,7 @@ impl RedisCache {
|
|
103 |
.await;
|
104 |
continue;
|
105 |
}
|
106 |
-
false => return Err(Report::new(
|
107 |
},
|
108 |
Ok(res) => return Ok(res),
|
109 |
}
|
@@ -127,7 +127,7 @@ impl RedisCache {
|
|
127 |
&mut self,
|
128 |
json_results: &str,
|
129 |
url: &str,
|
130 |
-
) -> Result<(), Report<
|
131 |
self.current_connection = Default::default();
|
132 |
let hashed_url_string: &str = &self.hash_url(url);
|
133 |
|
@@ -150,7 +150,7 @@ impl RedisCache {
|
|
150 |
self.current_connection += 1;
|
151 |
if self.current_connection == self.pool_size {
|
152 |
return Err(Report::new(
|
153 |
-
|
154 |
));
|
155 |
}
|
156 |
result = self.connection_pool[self.current_connection as usize]
|
@@ -158,7 +158,7 @@ impl RedisCache {
|
|
158 |
.await;
|
159 |
continue;
|
160 |
}
|
161 |
-
false => return Err(Report::new(
|
162 |
},
|
163 |
Ok(_) => return Ok(()),
|
164 |
}
|
|
|
6 |
use md5::compute;
|
7 |
use redis::{aio::ConnectionManager, AsyncCommands, Client, RedisError};
|
8 |
|
9 |
+
use super::error::CacheError;
|
10 |
|
11 |
/// A named struct which stores the redis Connection url address to which the client will
|
12 |
/// connect to.
|
|
|
72 |
///
|
73 |
/// Returns the results as a String from the cache on success otherwise returns a `CacheError`
|
74 |
/// on a failure.
|
75 |
+
pub async fn cached_json(&mut self, url: &str) -> Result<String, Report<CacheError>> {
|
76 |
self.current_connection = Default::default();
|
77 |
let hashed_url_string: &str = &self.hash_url(url);
|
78 |
|
|
|
95 |
self.current_connection += 1;
|
96 |
if self.current_connection == self.pool_size {
|
97 |
return Err(Report::new(
|
98 |
+
CacheError::PoolExhaustionWithConnectionDropError,
|
99 |
));
|
100 |
}
|
101 |
result = self.connection_pool[self.current_connection as usize]
|
|
|
103 |
.await;
|
104 |
continue;
|
105 |
}
|
106 |
+
false => return Err(Report::new(CacheError::RedisError(error))),
|
107 |
},
|
108 |
Ok(res) => return Ok(res),
|
109 |
}
|
|
|
127 |
&mut self,
|
128 |
json_results: &str,
|
129 |
url: &str,
|
130 |
+
) -> Result<(), Report<CacheError>> {
|
131 |
self.current_connection = Default::default();
|
132 |
let hashed_url_string: &str = &self.hash_url(url);
|
133 |
|
|
|
150 |
self.current_connection += 1;
|
151 |
if self.current_connection == self.pool_size {
|
152 |
return Err(Report::new(
|
153 |
+
CacheError::PoolExhaustionWithConnectionDropError,
|
154 |
));
|
155 |
}
|
156 |
result = self.connection_pool[self.current_connection as usize]
|
|
|
158 |
.await;
|
159 |
continue;
|
160 |
}
|
161 |
+
false => return Err(Report::new(CacheError::RedisError(error))),
|
162 |
},
|
163 |
Ok(_) => return Ok(()),
|
164 |
}
|
tests/index.rs
CHANGED
@@ -12,6 +12,7 @@ fn spawn_app() -> String {
|
|
12 |
let server = run(
|
13 |
listener,
|
14 |
config,
|
|
|
15 |
websurfx::cache::cacher::Cache::new_in_memory(),
|
16 |
)
|
17 |
.expect("Failed to bind address");
|
|
|
12 |
let server = run(
|
13 |
listener,
|
14 |
config,
|
15 |
+
#[cfg(all(feature = "memory-cache", not(feature = "redis-cache")))]
|
16 |
websurfx::cache::cacher::Cache::new_in_memory(),
|
17 |
)
|
18 |
.expect("Failed to bind address");
|