add: app data query cache option

This commit is contained in:
trisua 2025-08-17 14:52:29 -04:00
parent fdbc08912d
commit 8116307ba0
11 changed files with 100 additions and 49 deletions

View file

@ -27,7 +27,7 @@ tetratto-shared = { path = "../shared" }
tetratto-core = { path = "../core" }
tetratto-l10n = { path = "../l10n" }
image = "0.25.6"
reqwest = { version = "0.12.22", features = ["json", "stream"] }
reqwest = { version = "0.12.23", features = ["json", "stream"] }
regex = "1.11.1"
serde_json = "1.0.142"
mime_guess = "2.0.5"
@ -42,7 +42,7 @@ async-stripe = { version = "0.41.0", features = [
"runtime-tokio-hyper",
"connect",
] }
emojis = "0.7.1"
emojis = "0.7.2"
webp = "0.3.0"
nanoneo = "0.2.0"
cookie = "0.18.1"

View file

@ -65,7 +65,11 @@
(option
("value" "Tier3")
("selected" "{% if app.storage_capacity == 'Tier3' -%}true{% else %}false{%- endif %}")
(text "Tier 3 (100 MB)")))))
(text "Tier 3 (100 MB)"))
(option
("value" "Unlimited")
("selected" "{% if app.storage_capacity == 'Unlimited' -%}true{% else %}false{%- endif %}")
(text "Unlimited")))))
(text "{%- endif %}")
(div
("class" "card_nest")

View file

@ -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()),
}
}

View file

@ -1276,6 +1276,7 @@ pub struct InsertAppData {
pub struct QueryAppData {
pub query: AppDataSelectQuery,
pub mode: AppDataSelectMode,
pub cache: bool,
}
#[derive(Deserialize)]

View file

@ -1,7 +1,7 @@
[package]
name = "tetratto-core"
description = "The core behind Tetratto"
version = "15.0.0"
version = "15.0.1"
edition = "2024"
authors.workspace = true
repository.workspace = true
@ -23,7 +23,7 @@ default = ["database", "types", "sdk"]
[dependencies]
pathbufd = "0.1.4"
serde = { version = "1.0.219", features = ["derive"] }
toml = "0.9.4"
toml = "0.9.5"
tetratto-shared = { version = "12.0.6", path = "../shared" }
tetratto-l10n = { version = "12.0.0", path = "../l10n" }
serde_json = "1.0.142"
@ -31,16 +31,16 @@ totp-rs = { version = "5.7.0", features = [
"qr",
"gen_secret",
], optional = true }
reqwest = { version = "0.12.22", features = [
reqwest = { version = "0.12.23", features = [
"json",
"multipart",
], optional = true }
bitflags = { version = "2.9.1", optional = true }
bitflags = { version = "2.9.2", optional = true }
async-recursion = { version = "1.1.1", optional = true }
md-5 = { version = "0.10.6", optional = true }
base16ct = { version = "0.2.0", features = ["alloc"], optional = true }
base64 = { version = "0.22.1", optional = true }
emojis = "0.7.1"
emojis = "0.7.2"
regex = "1.11.1"
oiseau = { version = "0.1.2", default-features = false, features = [
"postgres",

View file

@ -28,6 +28,7 @@ pub async fn main() {
.query(&SimplifiedQuery {
query: AppDataSelectQuery::KeyIs("rust_test".to_string()),
mode: AppDataSelectMode::One(0),
cache: false,
})
.await
.unwrap()
@ -49,6 +50,7 @@ pub async fn main() {
.query(&SimplifiedQuery {
query: AppDataSelectQuery::KeyIs("rust_test".to_string()),
mode: AppDataSelectMode::One(0),
cache: false,
})
.await
.unwrap()

View file

@ -32,6 +32,8 @@ pub enum DeveloperPassStorageQuota {
Tier2,
/// The app is limited to 100 MB.
Tier3,
/// The app is not limited.
Unlimited,
}
impl Default for DeveloperPassStorageQuota {
@ -46,6 +48,7 @@ impl DeveloperPassStorageQuota {
DeveloperPassStorageQuota::Tier1 => 26214400,
DeveloperPassStorageQuota::Tier2 => 52428800,
DeveloperPassStorageQuota::Tier3 => 104857600,
DeveloperPassStorageQuota::Unlimited => usize::MAX,
}
}
}

View file

@ -31,6 +31,7 @@ macro_rules! api_return_ok {
pub struct SimplifiedQuery {
pub query: AppDataSelectQuery,
pub mode: AppDataSelectMode,
pub cache: bool,
}
/// The data client is used to access an app's data storage capabilities.

View file

@ -11,4 +11,4 @@ homepage.workspace = true
[dependencies]
pathbufd = "0.1.4"
serde = { version = "1.0.219", features = ["derive"] }
toml = "0.9.4"
toml = "0.9.5"

View file

@ -17,4 +17,4 @@ regex = "1.11.1"
serde = { version = "1.0.219", features = ["derive"] }
sha2 = "0.10.9"
snowflaked = "1.0.3"
uuid = { version = "1.17.0", features = ["v4"] }
uuid = { version = "1.18.0", features = ["v4"] }