- 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:
2024-11-25 22:20:15 -05:00
parent 3ff22c9a5b
commit 3fc6c6252c
15 changed files with 181 additions and 103 deletions

View File

@@ -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((