Skip to content

Commit 2a82dcc

Browse files
committed
feat(fetch): add cache tags support to fetch operations
1 parent bb02ea0 commit 2a82dcc

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

crates/rari/src/runtime/ext/web/init_fetch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ async function fetchWithRustCache(input, init, meta) {
113113
options.cacheTTLMs = String(revalidate * 1000)
114114
}
115115

116+
const tags = init?.rari?.tags ?? init?.next?.tags
117+
if (tags && Array.isArray(tags) && tags.length > 0) {
118+
options.tags = JSON.stringify(tags)
119+
}
120+
116121
const timeoutMs = init?.rari?.timeout ?? init?.fetchOptions?.timeout ?? 5000
117122
options.timeout = String(typeof timeoutMs === 'number' && timeoutMs > 0 ? timeoutMs : 5000)
118123

crates/rari/src/runtime/ops.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ pub async fn op_fetch_with_cache(
295295
"statusText": http_status_text(result.status),
296296
"body": body_str,
297297
"headers": headers_obj,
298-
"cached": result.was_cached
298+
"cached": result.was_cached,
299+
"tags": result.tags
299300
}))
300301
}
301302
Err(e) => {

crates/rari/src/server/middleware/request_context.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct CachedFetchResult {
1919
pub headers: HeaderMap,
2020
pub cached_at: Instant,
2121
pub was_cached: bool,
22+
pub tags: Vec<String>,
2223
}
2324
type InFlightFetches =
2425
Arc<DashMap<String, Arc<TokioMutex<Option<Result<CachedFetchResult, RariError>>>>>>;
@@ -77,7 +78,7 @@ impl RequestContext {
7778
fn generate_cache_key(url: &str, options: &FxHashMap<String, String>) -> String {
7879
let cache_relevant_options: FxHashMap<_, _> = options
7980
.iter()
80-
.filter(|(k, _)| !matches!(k.as_str(), "cacheTTLMs" | "timeout"))
81+
.filter(|(k, _)| !matches!(k.as_str(), "cacheTTLMs" | "timeout" | "tags"))
8182
.collect();
8283

8384
if cache_relevant_options.is_empty() {
@@ -113,6 +114,9 @@ impl RequestContext {
113114
) -> Result<CachedFetchResult, RariError> {
114115
let cache_key = Self::generate_cache_key(url, &options);
115116

117+
let tags: Vec<String> =
118+
options.get("tags").and_then(|t| serde_json::from_str(t).ok()).unwrap_or_default();
119+
116120
{
117121
let mut cache = self.fetch_cache.lock();
118122
if let Some(cached) = cache.get(&cache_key) {
@@ -157,7 +161,11 @@ impl RequestContext {
157161
cache_key: cache_key.clone(),
158162
};
159163

160-
let fetch_result = self.perform_fetch(url, &options).await;
164+
let mut fetch_result = self.perform_fetch(url, &options).await;
165+
166+
if let Ok(ref mut result) = fetch_result {
167+
result.tags = tags;
168+
}
161169

162170
*guard = Some(fetch_result.clone());
163171

@@ -210,6 +218,7 @@ impl RequestContext {
210218
headers,
211219
cached_at: Instant::now(),
212220
was_cached: false,
221+
tags: Vec::new(),
213222
})
214223
}
215224
}
@@ -240,6 +249,7 @@ mod tests {
240249
headers: HeaderMap::new(),
241250
cached_at: Instant::now(),
242251
was_cached: false,
252+
tags: Vec::new(),
243253
};
244254

245255
let test_key = format!("https://test-{}.example.com", uuid::Uuid::new_v4());

0 commit comments

Comments
 (0)