|
1 | 1 | use std::time::Duration;
|
2 | 2 | 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}; |
16 | 5 |
|
17 | 6 | #[derive(Debug)]
|
18 | 7 | 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 |
24 | 15 | pub file_bytes: moka::future::Cache<String, Bytes>,
|
25 | 16 | }
|
26 | 17 |
|
27 | 18 | impl Default for Cache {
|
28 | 19 | 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); |
42 | 44 | }
|
43 | 45 | }
|
0 commit comments