- update: database client to implement a database trait so we can mock it out
- update unit tests - add: database client mock
This commit is contained in:
@@ -2,18 +2,23 @@ use deadpool_redis::{Config, Pool, Runtime};
|
||||
use redis::{AsyncCommands, RedisError};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
#[async_trait::async_trait]
|
||||
pub trait Cache {
|
||||
fn new(redis_url: &str) -> Self;
|
||||
}
|
||||
|
||||
pub struct RedisCache {
|
||||
pub pool: Pool,
|
||||
}
|
||||
|
||||
impl RedisCache {
|
||||
pub fn new(redis_url: &str) -> Self {
|
||||
impl Cache for RedisCache {
|
||||
fn new(redis_url: &str) -> Self {
|
||||
let cfg = Config::from_url(redis_url);
|
||||
let pool = cfg.create_pool(Some(Runtime::Tokio1)).expect("Failed to create Redis pool");
|
||||
RedisCache { pool }
|
||||
}
|
||||
|
||||
pub async fn set<T: Serialize + std::marker::Send + std::marker::Sync>(
|
||||
async fn set<T: Serialize + std::marker::Send + std::marker::Sync>(
|
||||
&self,
|
||||
key: &String,
|
||||
value: &T,
|
||||
@@ -36,7 +41,7 @@ impl RedisCache {
|
||||
conn.set_ex(key, serialized_value, ttl).await
|
||||
}
|
||||
|
||||
pub async fn get<T: DeserializeOwned>(&self, key: &String) -> Result<Option<T>, redis::RedisError> {
|
||||
async fn get<T: DeserializeOwned>(&self, key: &String) -> Result<Option<T>, redis::RedisError> {
|
||||
let mut conn = self.pool.get().await
|
||||
.map_err(|err| {
|
||||
redis::RedisError::from((
|
||||
|
||||
Reference in New Issue
Block a user