implemented working cache

some testing still has to be done
This commit is contained in:
2025-03-16 02:56:54 -04:00
parent 328dacb675
commit d126fed2bd
5 changed files with 457 additions and 71 deletions

View File

@@ -1,5 +1,5 @@
use super::posts::serialize_datetime;
use crate::{datasources::comments::CommentsDatasource, AppState};
use super::posts::{deserialize_datetime, serialize_datetime};
use crate::{datasources::comments::CommentsDatasource, state::AppState};
use axum::{
extract::{Path, State},
http::StatusCode,
@@ -8,33 +8,34 @@ use axum::{
Json,
};
use chrono::Utc;
use fred::types::{Expiration, SetOptions};
use serde::{Deserialize, Serialize};
use sqlx::{Pool, Postgres};
#[derive(Deserialize, Debug)]
#[derive(Deserialize, Serialize, Debug)]
pub struct CommentInputPayload {
pub name: String,
pub body: String,
pub post_id: i32,
}
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct CommentPathParams {
id: i32,
}
#[derive(Deserialize)]
#[derive(Deserialize, Serialize)]
pub struct Pagination {
pub page_number: i64,
pub page_size: i64,
}
#[derive(sqlx::FromRow, Serialize, Debug)]
#[derive(sqlx::FromRow, Deserialize, Serialize, Debug, Clone)]
pub struct Comment {
pub comment_id: i32,
pub name: String,
pub body: String,
#[serde(serialize_with = "serialize_datetime")]
#[serde(deserialize_with = "deserialize_datetime")]
pub created_at: Option<chrono::DateTime<Utc>>,
}
@@ -46,34 +47,61 @@ impl CommentsRoute {
.route("/post/:id", get(CommentsRoute::get_post_comments))
.route("/add", post(CommentsRoute::insert_comment))
.route("/index", get(CommentsRoute::get_comments_index))
.with_state(app_state.db.clone())
.with_state(app_state.clone())
}
async fn get_post_comments(
State(pool): State<Pool<Postgres>>,
State(app_state): State<AppState>,
Path(params): Path<CommentPathParams>,
) -> impl IntoResponse {
match CommentsDatasource::get_posts_comments(&pool, params.id).await {
let state = app_state.lock().await;
match CommentsDatasource::get_posts_comments(&state.database, params.id).await {
Ok(c) => Ok(Json(c)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
//
async fn insert_comment(
State(pool): State<Pool<Postgres>>,
State(app_state): State<AppState>,
Json(input): Json<CommentInputPayload>,
) -> impl IntoResponse {
match CommentsDatasource::insert_comment(&pool, input).await {
Ok(c) => Ok((StatusCode::CREATED, Json(c))),
let state = app_state.lock().await;
match CommentsDatasource::insert_comment(&state.database, input).await {
Ok(c) => {
if let co = &c {
let co = c.clone();
let state = app_state.clone();
tokio::spawn(async move {
tracing::info!("update cache if key already exists!");
let mut s = state.lock().await;
let _ = s
.cache
.set(
format!("comments:{}", co.comment_id),
&co,
Some(Expiration::EX(60 * 15)),
Some(SetOptions::XX),
false,
)
.await;
});
}
Ok((StatusCode::CREATED, Json(c)))
}
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}
}
async fn get_comments_index(
State(pool): State<Pool<Postgres>>,
State(app_state): State<AppState>,
Json(pagination): Json<Pagination>,
) -> impl IntoResponse {
match CommentsDatasource::get_index_comments(&pool, pagination).await {
let state = app_state.lock().await;
match CommentsDatasource::get_index_comments(&state.database, pagination).await {
Ok(c) => Ok(Json(c)),
Err(e) => Err((StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
}