Skip to content

Commit

Permalink
chore: cache
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Dec 1, 2024
1 parent 034ce68 commit 2b575ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 27 deletions.
15 changes: 3 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description = "The CdsCTF project is an open-source, high-performance, Jeopardy-
async-trait = { version = "0.1" }
tokio = { version = "1.41", features = ["full"] }
tokio-util = { version = "0.7.12" }
tokio-rustls = { version = "0.26.0", features = ["ring"] }
futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
tower = { version = "0.5" }
Expand All @@ -25,13 +24,6 @@ axum = { version = "0.7", features = [
"tracing",
"json",
] }
axum-extra = { version = "0.9", features = [
"typed-header",
"query",
"multipart",
"typed-routing",
"async-read-body",
] }
rust-embed = { version = "8.5" }
mime = { version = "0.3" }
mime_guess = { version = "2.0" }
Expand Down Expand Up @@ -76,14 +68,13 @@ sea-orm = { version = "1.1", features = [
"with-json",
"runtime-tokio-rustls",
] }
sea-orm-migration = { version = "1.1" }

# Message Queue
async-nats = { version = "0.38" }

# Cache
fred = { version = "9.4", features = [
"enable-rustls",
fred = { version = "10.0", features = [
"enable-rustls-ring",
"dns",
"mocks",
"monitor",
Expand All @@ -104,7 +95,7 @@ reqwest = { version = "0.12", features = [
"json",
"rustls-tls",
], default-features = false }
sysinfo = { version = "0.32.0" }
sysinfo = { version = "0.32.1" }
tempfile = { version = "3.14.0" }
image = { version = "0.25.5" }
webp = { version = "0.3.0", features = ["image"] }
Expand Down
22 changes: 10 additions & 12 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fmt::Display;

use fred::{
prelude::{ClientLike, KeysInterface, RedisClient},
types::{Expiration, RedisConfig, RedisKey},
prelude::{Client, ClientLike, KeysInterface},
types::{config::Config, Expiration, Key},
};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
Expand All @@ -12,13 +12,13 @@ use traits::CacheError;

pub mod traits;

static CLIENT: OnceCell<RedisClient> = OnceCell::new();
static CLIENT: OnceCell<Client> = OnceCell::new();

fn get_client() -> RedisClient {
fn get_client() -> Client {
CLIENT.get().unwrap().clone()
}

pub async fn get<T>(key: impl Into<RedisKey> + Send + Display) -> Result<Option<T>, CacheError>
pub async fn get<T>(key: impl Into<Key> + Send + Display) -> Result<Option<T>, CacheError>
where
T: for<'de> Deserialize<'de>, {
let result = get_client().get::<Option<Value>, _>(key).await?;
Expand All @@ -28,9 +28,7 @@ where
}
}

pub async fn get_del<T>(
key: impl Into<RedisKey> + Send + Display,
) -> Result<Option<T>, CacheError>
pub async fn get_del<T>(key: impl Into<Key> + Send + Display) -> Result<Option<T>, CacheError>
where
T: for<'de> Deserialize<'de>, {
let result = get_client().getdel::<Option<Value>, _>(key).await?;
Expand All @@ -41,7 +39,7 @@ where
}

pub async fn set(
key: impl Into<RedisKey> + Send + Display, value: impl Serialize + Send,
key: impl Into<Key> + Send + Display, value: impl Serialize + Send,
) -> Result<(), CacheError> {
let value = serde_json::to_string(&value)?;
get_client().set(key, value, None, None, false).await?;
Expand All @@ -50,7 +48,7 @@ pub async fn set(
}

pub async fn set_ex(
key: impl Into<RedisKey> + Send + Display, value: impl Serialize + Send, expire: u64,
key: impl Into<Key> + Send + Display, value: impl Serialize + Send, expire: u64,
) -> Result<(), CacheError> {
let value = serde_json::to_string(&value)?;
get_client()
Expand All @@ -67,8 +65,8 @@ pub async fn flush() -> Result<(), CacheError> {
}

pub async fn init() {
let config = RedisConfig::from_url(&crate::env::get_env().cache.url).unwrap();
let client = RedisClient::new(config, None, None, None);
let config = Config::from_url(&crate::env::get_env().cache.url).unwrap();
let client = Client::new(config, None, None, None);
client.init().await.unwrap();

CLIENT.set(client).unwrap();
Expand Down
3 changes: 1 addition & 2 deletions src/cache/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use fred::error::RedisError;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum CacheError {
#[error("redis error: {0}")]
RedisError(#[from] RedisError),
RedisError(#[from] fred::error::Error),
#[error("serde_json error: {0}")]
SerdeJsonError(#[from] serde_json::Error),
}
3 changes: 2 additions & 1 deletion src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub(crate)mod traits;
pub mod traits;

use std::{collections::BTreeMap, process, sync::OnceLock, time::Duration};

Expand All @@ -19,6 +19,7 @@ use kube::{
use once_cell::sync::OnceCell;
use tokio_util::codec::Framed;
use tracing::{error, info};

use crate::cluster::traits::ClusterError;

static K8S_CLIENT: OnceCell<K8sClient> = OnceCell::new();
Expand Down

0 comments on commit 2b575ab

Please sign in to comment.