Skip to content

Commit

Permalink
fixed tests and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
gfusee committed Oct 20, 2024
1 parent 1ab0e0d commit dc02e4e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion caching/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
6 changes: 4 additions & 2 deletions caching/src/local/caching_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<redis::Client>;

Expand Down Expand Up @@ -53,15 +50,15 @@ impl<Client: RedisClient> CachingStrategy for BaseCachingRedis<Client> {
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())
};

Ok(Some(decoded))
}

async fn set_cache<T: Serialize + DeserializeOwned + Send + Sync>(&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())
};

Expand Down Expand Up @@ -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;
Expand All @@ -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<K: ToRedisArgs + Send + Sync, V: ToRedisArgs + Send + Sync>(&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!();
}

Expand Down
4 changes: 2 additions & 2 deletions caching/src/redis/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit dc02e4e

Please sign in to comment.