Skip to content

Commit 25d6886

Browse files
committed
Update routing
1 parent 139e829 commit 25d6886

File tree

4 files changed

+165
-279
lines changed

4 files changed

+165
-279
lines changed

engine/src/cache.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
11
use std::time::Duration;
22
use bytes::Bytes;
3-
use serde::{Serialize, Deserialize};
4-
use crate::models::deployment::DeploymentFileEntry;
5-
6-
/// Result of resolving a host and path to a deployment file entry or an error reason.
7-
#[derive(Clone, Debug, Serialize, Deserialize)]
8-
pub enum ResolveResult {
9-
/// Successfully found a deployment file entry.
10-
Success(DeploymentFileEntry),
11-
/// The resource was not found, with a reason message.
12-
NotFound(String),
13-
/// An internal error occurred during resolution, with an error message.
14-
Error(String),
15-
}
3+
use serde_json::Value;
4+
use crate::models::deployment::{Deployment, DeploymentFileEntry};
165

176
#[derive(Debug)]
187
pub struct Cache {
19-
// pub raw: DashMap<String, Shared<BoxFuture<'static, CachedValue<serde_json::Value>>>>,
20-
pub raw: moka::future::Cache<String, serde_json::Value>,
21-
/// Cache for HTTP resolution results (domain + path).
22-
pub resolve: moka::future::Cache<String, ResolveResult>,
23-
/// Cache for bytes of small files to avoid repeated S3 fetches.
8+
/// Raw JSON-based cache used elsewhere in the app.
9+
pub raw: moka::future::Cache<String, Value>,
10+
/// Cache domain -> Deployment
11+
pub domain: moka::future::Cache<String, Option<Deployment>>,
12+
/// Cache deployment_id:path -> DeploymentFileEntry
13+
pub file_entry: moka::future::Cache<String, Option<DeploymentFileEntry>>,
14+
/// Cache file_hash -> Bytes for small files
2415
pub file_bytes: moka::future::Cache<String, Bytes>,
2516
}
2617

2718
impl Default for Cache {
2819
fn default() -> Self {
29-
Self {
30-
raw: moka::future::Cache::builder()
31-
.max_capacity(1000)
32-
.time_to_live(Duration::from_secs(10))
33-
.build(),
34-
resolve: moka::future::Cache::builder()
35-
.max_capacity(1000)
36-
.time_to_live(Duration::from_secs(60)) // TTL for resolution cache
37-
.build(),
38-
file_bytes: moka::future::Cache::builder()
39-
.max_capacity(1000)
40-
.build(),
41-
}
20+
let raw = moka::future::Cache::builder()
21+
.max_capacity(1000)
22+
.time_to_live(Duration::from_secs(10))
23+
.build();
24+
let domain = moka::future::Cache::builder()
25+
.max_capacity(1000)
26+
.time_to_live(Duration::from_secs(300))
27+
.build();
28+
let file_entry = moka::future::Cache::builder()
29+
.max_capacity(10000)
30+
.time_to_live(Duration::from_secs(60))
31+
.build();
32+
let file_bytes = moka::future::Cache::builder()
33+
.max_capacity(1000)
34+
.time_to_live(Duration::from_secs(60 * 60))
35+
.build();
36+
Self { raw, domain, file_entry, file_bytes }
37+
}
38+
}
39+
40+
impl Cache {
41+
/// Invalidate cached deployment for this domain.
42+
pub fn bump_domain(&self, domain: &str) {
43+
self.domain.invalidate(domain);
4244
}
4345
}

engine/src/models/deployment/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616

1717
pub mod preview;
1818

19-
#[derive(Debug, Serialize, Deserialize, Object)]
19+
#[derive(Clone, Debug, Serialize, Deserialize, Object)]
2020
#[oai(example)]
2121
pub struct Deployment {
2222
/// The Deployment ID
@@ -341,7 +341,7 @@ impl DeploymentFile {
341341
}
342342

343343
// Add this new struct to represent the joined result
344-
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow, Object, Clone)]
344+
#[derive(Clone, Debug, Serialize, Deserialize, sqlx::FromRow, Object)]
345345
pub struct DeploymentFileEntry {
346346
pub deployment_file_deployment_id: String,
347347
pub deployment_file_file_id: i64,

engine/src/routes/site/deployments/mod.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tracing::info;
55
use crate::{
66
handlers::car::CarRequest, middlewares::auth::UserAuth, models::{
77
deployment::{preview::DeploymentPreview, Deployment, DeploymentFile, DeploymentFileEntry},
8-
domain::Domain,
8+
domain::{Domain, DomainSubmission},
99
site::{Site, SiteId},
1010
}, routes::{error::HttpError, ApiTags}, state::State
1111
};
@@ -105,7 +105,7 @@ impl SiteDeploymentsApi {
105105

106106
info!("Uploading file: {:?}", payload.data);
107107

108-
let deployment = Deployment::new(&state.database, site_id, payload.context)
108+
let deployment = Deployment::new(&state.database, site_id.clone(), payload.context)
109109
.await
110110
.map_err(HttpError::from)?;
111111

@@ -133,6 +133,14 @@ impl SiteDeploymentsApi {
133133
Deployment::upload_files(&deployment, &state, data)
134134
.await
135135
.unwrap();
136+
137+
// Invalidate cache for all verified domains of this site
138+
let site_id = site_id.clone();
139+
if let Ok(domains) = Domain::get_by_site_id(&site_id, &*state).await {
140+
for ds in domains.into_iter() {
141+
state.cache.bump_domain(&ds.domain());
142+
}
143+
}
136144
}
137145

138146
// let cutoff_date = Utc::now() - Duration::days(365);
@@ -196,6 +204,13 @@ impl SiteDeploymentsApi {
196204
Deployment::upload_files(&deployment, &state, data)
197205
.await
198206
.unwrap();
207+
208+
// Invalidate cache for verified domains on file update
209+
if let Ok(domains) = Domain::get_by_site_id(&site_id.0, &*state).await {
210+
for ds in domains.into_iter() {
211+
state.cache.bump_domain(&ds.domain());
212+
}
213+
}
199214
}
200215

201216
// update context on deployment

0 commit comments

Comments
 (0)