add: app data query cache option
This commit is contained in:
parent
fdbc08912d
commit
8116307ba0
11 changed files with 100 additions and 49 deletions
|
@ -4,10 +4,14 @@ use crate::{
|
|||
State,
|
||||
};
|
||||
use axum::{extract::Path, http::HeaderMap, response::IntoResponse, Extension, Json};
|
||||
use tetratto_core::model::{
|
||||
apps::{AppData, AppDataQuery, AppDataQueryResult},
|
||||
ApiReturn, Error,
|
||||
use tetratto_core::{
|
||||
cache::Cache,
|
||||
model::{
|
||||
apps::{AppData, AppDataQuery, AppDataQueryResult},
|
||||
ApiReturn, Error,
|
||||
},
|
||||
};
|
||||
use tetratto_shared::hash::hash;
|
||||
|
||||
pub async fn get_app_request(
|
||||
headers: HeaderMap,
|
||||
|
@ -37,6 +41,24 @@ pub async fn query_request(
|
|||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let query_hash = hash(serde_json::to_string(&req.query).unwrap());
|
||||
let redis_query_key = format!("atto.app.{}:{query_hash}", app.id);
|
||||
|
||||
if req.cache {
|
||||
if let Some(x) = data.0.1.get(redis_query_key.clone()).await {
|
||||
match serde_json::from_str::<AppDataQueryResult>(&x) {
|
||||
Ok(x) => {
|
||||
return Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: Some(x),
|
||||
});
|
||||
}
|
||||
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match data
|
||||
.query_app_data(AppDataQuery {
|
||||
app: app.id,
|
||||
|
@ -45,11 +67,26 @@ pub async fn query_request(
|
|||
})
|
||||
.await
|
||||
{
|
||||
Ok(x) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: Some(x),
|
||||
}),
|
||||
Ok(x) => {
|
||||
// store
|
||||
if req.cache {
|
||||
if !data
|
||||
.0
|
||||
.1
|
||||
.set(redis_query_key, serde_json::to_string(&x).unwrap())
|
||||
.await
|
||||
{
|
||||
return Json(Error::Unknown.into());
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: Some(x),
|
||||
})
|
||||
}
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1276,6 +1276,7 @@ pub struct InsertAppData {
|
|||
pub struct QueryAppData {
|
||||
pub query: AppDataSelectQuery,
|
||||
pub mode: AppDataSelectMode,
|
||||
pub cache: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue