From dc02e4ee9f4b3cd902f16d17e6cee03a703e1308 Mon Sep 17 00:00:00 2001 From: quentin Date: Sun, 20 Oct 2024 12:47:24 +0300 Subject: [PATCH] fixed tests and clippy --- caching/Cargo.toml | 2 +- caching/src/local/caching_local.rs | 6 ++++-- caching/src/redis/{redis.rs => caching_redis.rs} | 15 ++++++--------- caching/src/redis/mod.rs | 4 ++-- 4 files changed, 13 insertions(+), 14 deletions(-) rename caching/src/redis/{redis.rs => caching_redis.rs} (94%) diff --git a/caching/Cargo.toml b/caching/Cargo.toml index e7cfc04..137d3cb 100644 --- a/caching/Cargo.toml +++ b/caching/Cargo.toml @@ -14,7 +14,7 @@ tokio = "1.29.1" async-trait = "0.1.72" redis = { version = "0.27.4", features = ["aio", "tokio-comp"] } novax = { path = "../core", version = "0.1.13" } -bitcode = { version = "=0.6.3", features = ["serde"] } +rmp-serde = "=1.1.2" [dev-dependencies] thread_local = "1.1.7" diff --git a/caching/src/local/caching_local.rs b/caching/src/local/caching_local.rs index 2c6d2f6..d2ae1be 100644 --- a/caching/src/local/caching_local.rs +++ b/caching/src/local/caching_local.rs @@ -44,7 +44,7 @@ impl CachingLocal { let expiration_timestamp = self.duration_strategy.get_duration_timestamp(&get_current_timestamp()?)?; self.expiration_timestamp_map.lock().await.insert(key, expiration_timestamp); - let Ok(serialized) = bitcode::serialize(value) else { return Err(CachingError::UnableToSerialize.into())}; + let Ok(serialized) = rmp_serde::to_vec(value) else { return Err(CachingError::UnableToSerialize.into())}; self.value_map.lock().await.insert(key, serialized); Ok(()) @@ -61,7 +61,9 @@ impl CachingStrategy for CachingLocal { Ok(None) } else { let Some(encoded_value) = self.value_map.lock().await.get(&key).cloned() else { return Ok(None) }; - let Ok(value) = bitcode::deserialize(&encoded_value) else { return Err(CachingError::UnableToDeserialize.into()) }; + let Ok(value) = rmp_serde::from_slice(&encoded_value) else { + return Err(CachingError::UnableToDeserialize.into()) + }; Ok(Some(value)) } diff --git a/caching/src/redis/redis.rs b/caching/src/redis/caching_redis.rs similarity index 94% rename from caching/src/redis/redis.rs rename to caching/src/redis/caching_redis.rs index f668828..e79ddeb 100644 --- a/caching/src/redis/redis.rs +++ b/caching/src/redis/caching_redis.rs @@ -1,10 +1,6 @@ use std::future::Future; use async_trait::async_trait; -pub use redis::ConnectionInfo; -pub use redis::IntoConnectionInfo; -pub use redis::RedisConnectionInfo; -pub use redis::RedisError; use serde::de::DeserializeOwned; use serde::Serialize; @@ -14,6 +10,7 @@ use novax::errors::{CachingError, NovaXError}; use crate::date::get_current_timestamp::{get_current_timestamp, GetDuration}; use crate::redis::client::RedisClient; use crate::redis::error::CachingRedisError; +use crate::redis::IntoConnectionInfo; pub type CachingRedis = BaseCachingRedis; @@ -53,7 +50,7 @@ impl CachingStrategy for BaseCachingRedis { return Ok(None); }; - let Ok(decoded) = bitcode::deserialize(&value_encoded) else { + let Ok(decoded) = rmp_serde::from_slice(&value_encoded) else { return Err(CachingError::UnableToDeserialize.into()) }; @@ -61,7 +58,7 @@ impl CachingStrategy for BaseCachingRedis { } async fn set_cache(&self, key: u64, value: &T) -> Result<(), NovaXError> { - let Ok(encoded) = bitcode::serialize(value) else { + let Ok(encoded) = rmp_serde::to_vec(value) else { return Err(CachingError::UnableToSerialize.into()) }; @@ -122,7 +119,7 @@ mod test { use crate::date::get_current_timestamp::set_mock_time; use crate::redis::client::RedisClient; use crate::redis::error::CachingRedisError; - use crate::redis::redis::BaseCachingRedis; + use crate::redis::caching_redis::BaseCachingRedis; #[derive(Clone, Debug)] struct MockRedisClient; @@ -137,14 +134,14 @@ mod test { if key.to_redis_args() == 1.to_redis_args() { // Not found Ok(None) } else if key.to_redis_args() == 2.to_redis_args() { // Found - Ok(Some(RV::from_byte_vec(&[2, 0, 1]).unwrap().into_iter().next().unwrap())) + Ok(Some(RV::from_byte_vec(&[146, 0, 1]).unwrap().into_iter().next().unwrap())) } else { Ok(None) } } async fn set(&self, key: K, value: V, duration: u64) -> Result<(), CachingRedisError> { - if value.to_redis_args() != bitcode::serialize("test").unwrap().to_redis_args() { + if value.to_redis_args() != rmp_serde::to_vec("test").unwrap().to_redis_args() { panic!(); } diff --git a/caching/src/redis/mod.rs b/caching/src/redis/mod.rs index 394eb15..05cc005 100644 --- a/caching/src/redis/mod.rs +++ b/caching/src/redis/mod.rs @@ -1,8 +1,8 @@ -pub mod redis; +pub mod caching_redis; pub mod client; pub mod error; pub use redis::ConnectionInfo; -pub use redis::RedisConnectionInfo; pub use redis::IntoConnectionInfo; +pub use redis::RedisConnectionInfo; pub use redis::RedisError; \ No newline at end of file