From 42dff3f186c2c3fce204cf6035ada200f87bce5c Mon Sep 17 00:00:00 2001 From: "Wyatt J. Miller" Date: Mon, 14 Jul 2025 20:26:17 -0400 Subject: [PATCH] switched public api to caching library --- backend/public/Cargo.lock | 26 +++++++--- backend/public/Cargo.toml | 1 + backend/public/src/routes/authors.rs | 2 +- backend/public/src/routes/comments.rs | 2 +- backend/public/src/routes/posts.rs | 2 +- backend/public/src/routes/projects.rs | 3 +- backend/public/src/state.rs | 72 ++------------------------- 7 files changed, 26 insertions(+), 82 deletions(-) diff --git a/backend/public/Cargo.lock b/backend/public/Cargo.lock index 51f31a9..cb12e5f 100644 --- a/backend/public/Cargo.lock +++ b/backend/public/Cargo.lock @@ -225,6 +225,15 @@ dependencies = [ "either", ] +[[package]] +name = "cache" +version = "0.1.0" +dependencies = [ + "fred", + "serde", + "serde_json", +] + [[package]] name = "cc" version = "1.1.21" @@ -1252,6 +1261,7 @@ name = "public" version = "0.1.0" dependencies = [ "axum", + "cache", "chrono", "dotenvy", "fred", @@ -1517,18 +1527,18 @@ checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0" [[package]] name = "serde" -version = "1.0.210" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.219" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", @@ -1537,9 +1547,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.128" +version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", @@ -1911,9 +1921,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.77" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", diff --git a/backend/public/Cargo.toml b/backend/public/Cargo.toml index b626412..14c54b3 100644 --- a/backend/public/Cargo.toml +++ b/backend/public/Cargo.toml @@ -25,3 +25,4 @@ serde_json = "1.0.128" chrono = "0.4.38" xml = "0.8.20" fred = "10.1.0" +cache = { version = "*", path = "../cache" } diff --git a/backend/public/src/routes/authors.rs b/backend/public/src/routes/authors.rs index 96dd1c1..30919a1 100644 --- a/backend/public/src/routes/authors.rs +++ b/backend/public/src/routes/authors.rs @@ -5,7 +5,7 @@ use axum::{ routing::get, Json, }; -use fred::types::Expiration; +use cache::Expiration; use serde::{Deserialize, Serialize}; use crate::{datasources::authors::AuthorsDatasource, state::AppState}; diff --git a/backend/public/src/routes/comments.rs b/backend/public/src/routes/comments.rs index a8a1ab1..0a8a660 100644 --- a/backend/public/src/routes/comments.rs +++ b/backend/public/src/routes/comments.rs @@ -6,8 +6,8 @@ use axum::{ routing::{get, post}, Json, }; +use cache::{Expiration, SetOptions}; use chrono::Utc; -use fred::types::{Expiration, SetOptions}; use serde::{Deserialize, Serialize}; #[derive(Deserialize, Serialize, Debug)] diff --git a/backend/public/src/routes/posts.rs b/backend/public/src/routes/posts.rs index 30c1e83..553a6c3 100644 --- a/backend/public/src/routes/posts.rs +++ b/backend/public/src/routes/posts.rs @@ -15,8 +15,8 @@ use axum::{ routing::get, Json, Router, }; +use cache::Expiration; use chrono::Utc; -use fred::types::Expiration; use serde::{Deserialize, Serialize}; use std::collections::HashMap; diff --git a/backend/public/src/routes/projects.rs b/backend/public/src/routes/projects.rs index 74dbcd6..c2367d7 100644 --- a/backend/public/src/routes/projects.rs +++ b/backend/public/src/routes/projects.rs @@ -1,7 +1,6 @@ use crate::{datasources::projects::ProjectsDatasource, state::AppState, utils::datetime::*}; -use axum::http::{HeaderMap, HeaderValue}; use axum::{extract::State, http::StatusCode, response::IntoResponse, routing::get, Json, Router}; -use fred::types::Expiration; +use cache::Expiration; use serde::{Deserialize, Serialize}; #[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)] diff --git a/backend/public/src/state.rs b/backend/public/src/state.rs index 751bfc6..77e920e 100644 --- a/backend/public/src/state.rs +++ b/backend/public/src/state.rs @@ -1,83 +1,17 @@ -use fred::interfaces::KeysInterface; -use fred::{clients::Pool, prelude::*}; use sqlx::PgPool; pub type AppState = std::sync::Arc>; pub struct AppInternalState { pub database: sqlx::postgres::PgPool, - pub cache: Cache, -} - -pub struct Cache { - pub inmem: Pool, + pub cache: cache::Cache, } impl AppInternalState { - pub fn new(database: PgPool, cache: Pool) -> Self { + pub fn new(database: PgPool, cache: cache::Pool) -> Self { AppInternalState { database, - cache: Cache { inmem: cache }, - } - } -} - -impl Cache { - pub async fn get(&mut self, key: String) -> Result, Box> - where - T: for<'de> serde::Deserialize<'de>, - { - self.is_connected()?; - let value: Option = self.inmem.get(&key).await?; - - match value { - Some(json_str) => match serde_json::from_str::(&json_str) { - Ok(deserialized) => Ok(Some(deserialized)), - Err(_) => Ok(None), - }, - None => Ok(None), - } - } - - pub async fn set( - &mut self, - key: String, - contents: &T, - expiration: Option, - set_opts: Option, - get: bool, - ) -> Result<(), Box> - where - T: for<'de> serde::Deserialize<'de> + serde::Serialize, - { - self.is_connected()?; - let json_string = match serde_json::to_string::(contents) { - Ok(s) => s, - Err(_) => { - return Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "Unable to deserialize contents passed to cache".to_string(), - ))) - } - }; - - Ok(self - .inmem - .set(key, json_string, expiration, set_opts, get) - .await?) - } - - pub async fn del(&mut self, key: String) -> Result<(), Box> { - Ok(self.inmem.del(key).await?) - } - - fn is_connected(&mut self) -> Result<(), Box> { - match self.inmem.is_connected() { - true => Ok(()), - false => Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - "Not connected to cache".to_string(), - ))), + cache: cache::Cache { inmem: cache }, } } }