athena_rs 3.3.0

Database gateway API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! Cache Check Module
//!
//! This module provides functionality to check and retrieve cached responses based on cache control headers.

use crate::AppState;
use crate::utils::redis_client::{
    GLOBAL_REDIS, note_redis_failure_and_start_cooldown, note_redis_success,
    redis_operation_timeout, should_bypass_redis_temporarily,
};
use actix_web::{HttpRequest, HttpResponse, web::Data};
use serde_json::{Value, json};
use std::time::Instant;

const RAW_CACHE_KEY_SUFFIX: &str = "__raw_json";

fn raw_cache_key(cache_key: &str) -> String {
    format!("{cache_key}:{RAW_CACHE_KEY_SUFFIX}")
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CacheLookupOutcome {
    BypassNoCacheHeader,
    HitLocalRaw,
    HitLocal,
    HitRedis,
    MissAllTiers,
    MissAfterRedisGetError,
    MissAfterRedisGetTimeout,
}

impl CacheLookupOutcome {
    pub fn as_str(self) -> &'static str {
        match self {
            CacheLookupOutcome::BypassNoCacheHeader => "bypass_no_cache_header",
            CacheLookupOutcome::HitLocalRaw => "hit_local_raw",
            CacheLookupOutcome::HitLocal => "hit_local",
            CacheLookupOutcome::HitRedis => "hit_redis",
            CacheLookupOutcome::MissAllTiers => "miss_all_tiers",
            CacheLookupOutcome::MissAfterRedisGetError => "miss_after_redis_get_error",
            CacheLookupOutcome::MissAfterRedisGetTimeout => "miss_after_redis_get_timeout",
        }
    }
}

/// ## Get Cached Response
///
/// This function retrieves a cached response for a given cache key.
///
/// ### Parameters
///
/// - `app_state`: A `Data<AppState>` instance containing the shared cache.
/// - `cache_key`: A string slice representing the key to look up in the cache.
///
/// ### Returns
///
/// - `Option<HttpResponse>`: Returns an `HttpResponse` containing the cached data if found, otherwise `None`.
///
/// ### Example
///
/// ```rust,no_run
/// # use actix_web::web::Data;
/// # use athena_rs::api::cache::check::get_cached_response;
/// # use athena_rs::AppState;
/// # use athena_rs::drivers::postgresql::sqlx_driver::PostgresClientRegistry;
/// # use moka::future::Cache;
/// # use reqwest::Client;
/// # use serde_json::json;
/// # use std::sync::Arc;
/// # use std::time::Instant;
/// # async fn doc_example() {
/// #     let cache = Cache::builder().build();
/// #     let immortal = Cache::builder().build();
/// #     let jdbc_pool_cache = Arc::new(Cache::builder().max_capacity(64).build());
/// #     let app_state = AppState {
/// #         cache: Arc::new(cache),
/// #         immortal_cache: Arc::new(immortal),
/// #         client: Client::new(),
/// #         process_start_time_seconds: 0,
/// #         process_started_at: Instant::now(),
/// #         pg_registry: Arc::new(PostgresClientRegistry::empty()),
/// #         jdbc_pool_cache,
/// #         #[cfg(feature = "deadpool_experimental")]
/// #         deadpool_registry: Arc::new(
/// #             athena_rs::drivers::postgresql::deadpool_registry::DeadpoolPostgresRegistry::empty(),
/// #         ),
/// #         #[cfg(feature = "deadpool_experimental")]
/// #         jdbc_deadpool_cache: Arc::new(Cache::builder().max_capacity(4).build()),
/// #         gateway_force_camel_case_to_snake_case: false,
/// #         gateway_auto_cast_uuid_filter_values_to_text: true,
/// #         gateway_allow_schema_names_prefixed_as_table_name: true,
/// #         pipeline_registry: None,
/// #         logging_client_name: None,
/// #         gateway_auth_client_name: None,
/// #         gateway_api_key_fail_mode: "fail_closed".to_string(),
/// #         gateway_jdbc_allow_private_hosts: false,
/// #         gateway_jdbc_allowed_hosts: Vec::new(),
/// #         gateway_resilience_timeout_secs: 30,
/// #         gateway_resilience_read_max_retries: 1,
/// #         gateway_resilience_initial_backoff_ms: 100,
/// #         gateway_admission_store_backend: "redis".to_string(),
/// #         gateway_admission_store_fail_mode: "fail_closed".to_string(),
/// #         prometheus_metrics_enabled: false,
/// #         metrics_state: Arc::new(athena_rs::api::metrics::MetricsState::new()),
/// #     };
/// #     let cache = Data::new(app_state);
/// let response = get_cached_response(cache, "some_cache_key").await;
/// #     let _ = response;
/// # }
/// ```
pub async fn get_cached_response(
    app_state: Data<AppState>,
    cache_key: &str,
) -> Option<HttpResponse> {
    let started_at: Instant = Instant::now();

    if let Some(cached_response) = app_state.cache.get(cache_key).await {
        return Some(HttpResponse::Ok().json(cached_response.clone()));
    }
    // Try Redis on local miss
    if let Some(redis) = GLOBAL_REDIS.get() {
        if should_bypass_redis_temporarily() {
            return None;
        }
        let redis_lookup_result =
            tokio::time::timeout(redis_operation_timeout(), redis.get(cache_key)).await;

        match redis_lookup_result {
            Ok(Ok(value)) if !value.is_null() => {
                note_redis_success();
                app_state
                    .cache
                    .insert(cache_key.to_string(), value.clone())
                    .await;
                return Some(HttpResponse::Ok().json(value));
            }
            Ok(Ok(_)) => note_redis_success(),
            Ok(Err(err)) => {
                note_redis_failure_and_start_cooldown();
                app_state.metrics_state.record_management_mutation(
                    "gateway_fetch_cache_lookup",
                    "redis_get_error",
                    started_at.elapsed().as_secs_f64(),
                );
                tracing::warn!(error = %err, cache_key = %cache_key, "Redis get failed; continuing with local cache only");
            }
            Err(_) => {
                note_redis_failure_and_start_cooldown();
                app_state.metrics_state.record_management_mutation(
                    "gateway_fetch_cache_lookup",
                    "redis_get_timeout",
                    started_at.elapsed().as_secs_f64(),
                );
                tracing::warn!(cache_key = %cache_key, "Redis get timed out; continuing with local cache only");
            }
        }
    }
    None
}

/// ## Check Cache Control and Get Response
///
/// This function checks if the "Cache-Control" header is set to "no-cache" and retrieves the cached response if not.
///
/// ### Parameters
///
/// - `req`: A reference to the `HttpRequest` object.
/// - `app_state`: A `Data<AppState>` instance containing the shared cache.
/// - `cache_key`: A string slice representing the key to look up in the cache.
///
/// ### Returns
///
/// - `Option<HttpResponse>`: Returns an `HttpResponse` containing the cached data if found and cache control is not "no-cache", otherwise `None`.
///
/// ### Example
///
/// ```rust,no_run
/// # use actix_web::http::header::CACHE_CONTROL;
/// # use actix_web::test::TestRequest;
/// # use actix_web::web::Data;
/// # use athena_rs::api::cache::check::check_cache_control_and_get_response;
/// # use athena_rs::AppState;
/// # use athena_rs::drivers::postgresql::sqlx_driver::PostgresClientRegistry;
/// # use moka::future::Cache;
/// # use reqwest::Client;
/// # use serde_json::json;
/// # use std::sync::Arc;
/// # use std::time::Instant;
/// # async fn doc_example() {
/// #     let cache = Cache::builder().build();
/// #     let immortal = Cache::builder().build();
/// #     let jdbc_pool_cache = Arc::new(Cache::builder().max_capacity(64).build());
/// #     let app_state = AppState {
/// #         cache: Arc::new(cache),
/// #         immortal_cache: Arc::new(immortal),
/// #         client: Client::new(),
/// #         process_start_time_seconds: 0,
/// #         process_started_at: Instant::now(),
/// #         pg_registry: Arc::new(PostgresClientRegistry::empty()),
/// #         jdbc_pool_cache,
/// #         #[cfg(feature = "deadpool_experimental")]
/// #         deadpool_registry: Arc::new(
/// #             athena_rs::drivers::postgresql::deadpool_registry::DeadpoolPostgresRegistry::empty(),
/// #         ),
/// #         #[cfg(feature = "deadpool_experimental")]
/// #         jdbc_deadpool_cache: Arc::new(Cache::builder().max_capacity(4).build()),
/// #         gateway_force_camel_case_to_snake_case: false,
/// #         gateway_auto_cast_uuid_filter_values_to_text: true,
/// #         gateway_allow_schema_names_prefixed_as_table_name: true,
/// #         pipeline_registry: None,
/// #         logging_client_name: None,
/// #         gateway_auth_client_name: None,
/// #         gateway_api_key_fail_mode: "fail_closed".to_string(),
/// #         gateway_jdbc_allow_private_hosts: false,
/// #         gateway_jdbc_allowed_hosts: Vec::new(),
/// #         gateway_resilience_timeout_secs: 30,
/// #         gateway_resilience_read_max_retries: 1,
/// #         gateway_resilience_initial_backoff_ms: 100,
/// #         gateway_admission_store_backend: "redis".to_string(),
/// #         gateway_admission_store_fail_mode: "fail_closed".to_string(),
/// #         prometheus_metrics_enabled: false,
/// #         metrics_state: Arc::new(athena_rs::api::metrics::MetricsState::new()),
/// #     };
/// #     let cache = Data::new(app_state);
/// #     let req = TestRequest::default()
/// #         .insert_header((CACHE_CONTROL, "max-age=0"))
/// #         .to_http_request();
/// let response = check_cache_control_and_get_response(&req, cache, "some_cache_key").await;
/// #     let _ = response;
/// # }
/// ```
pub async fn check_cache_control_and_get_response(
    req: &HttpRequest,
    app_state: Data<AppState>,
    cache_key: &str,
) -> Option<HttpResponse> {
    if let Some(cache_control_header) = req.headers().get("Cache-Control")
        && let Ok(cache_control_value) = cache_control_header.to_str()
        && cache_control_value.contains("no-cache")
    {
        return None;
    }

    if let Some(cached_response) = app_state.cache.get(cache_key).await {
        return Some(HttpResponse::Ok().json(json!({
            "success": true,
            "data": cached_response.clone()
        })));
    }
    None
}

/// ## Check Cache Control and Get Response V2
///
/// This function checks if the "Cache-Control" header is set to "no-cache" and retrieves the cached response if not.
///
/// ### Parameters
///
pub async fn check_cache_control_and_get_response_v2(
    req: &HttpRequest,
    app_state: Data<AppState>,
    cache_key: &str,
) -> Option<HttpResponse> {
    let (response, _outcome) = check_cache_control_and_get_response_v2_with_outcome(
        req,
        app_state,
        cache_key,
        "gateway_fetch_cache_lookup",
    )
    .await;
    response
}

/// `lookup_metric` is the first argument passed to [`crate::api::metrics::MetricsState::record_management_mutation`]
/// for cache lookup events (e.g. `gateway_fetch_cache_lookup` or `query_count_cache_lookup`).
pub async fn check_cache_control_and_get_response_v2_with_outcome(
    req: &HttpRequest,
    app_state: Data<AppState>,
    cache_key: &str,
    lookup_metric: &str,
) -> (Option<HttpResponse>, CacheLookupOutcome) {
    let started_at: Instant = Instant::now();

    if let Some(cache_control_header) = req.headers().get("Cache-Control")
        && let Ok(cache_control_value) = cache_control_header.to_str()
        && cache_control_value.contains("no-cache")
    {
        app_state.metrics_state.record_management_mutation(
            lookup_metric,
            CacheLookupOutcome::BypassNoCacheHeader.as_str(),
            started_at.elapsed().as_secs_f64(),
        );
        return (None, CacheLookupOutcome::BypassNoCacheHeader);
    }

    let raw_key: String = raw_cache_key(cache_key);
    if let Some(Value::String(raw_body)) = app_state.cache.get(&raw_key).await {
        let elapsed_secs = started_at.elapsed().as_secs_f64();
        app_state.metrics_state.record_management_mutation(
            lookup_metric,
            CacheLookupOutcome::HitLocalRaw.as_str(),
            elapsed_secs,
        );
        tracing::debug!(
            cache_key = %cache_key,
            source = "local_raw",
            duration_ms = started_at.elapsed().as_millis(),
            "gateway fetch cache hit"
        );
        return (
            Some(
                HttpResponse::Ok()
                    .content_type("application/json")
                    .body(raw_body),
            ),
            CacheLookupOutcome::HitLocalRaw,
        );
    }

    // Prefer local cache; on miss, try Redis
    if let Some(cached_response) = app_state.cache.get(cache_key).await {
        // If the response is an array with a single item, return just that item
        if let Value::Array(arr) = &cached_response
            && arr.len() == 1
        {
            let elapsed_secs = started_at.elapsed().as_secs_f64();
            app_state.metrics_state.record_management_mutation(
                lookup_metric,
                CacheLookupOutcome::HitLocal.as_str(),
                elapsed_secs,
            );
            tracing::debug!(
                cache_key = %cache_key,
                source = "local",
                duration_ms = started_at.elapsed().as_millis(),
                "gateway fetch cache hit"
            );
            return (
                Some(HttpResponse::Ok().json(&arr[0])),
                CacheLookupOutcome::HitLocal,
            );
        }

        let elapsed_secs = started_at.elapsed().as_secs_f64();
        app_state.metrics_state.record_management_mutation(
            lookup_metric,
            CacheLookupOutcome::HitLocal.as_str(),
            elapsed_secs,
        );
        tracing::debug!(
            cache_key = %cache_key,
            source = "local",
            duration_ms = started_at.elapsed().as_millis(),
            "gateway fetch cache hit"
        );
        return (
            Some(HttpResponse::Ok().json(cached_response)),
            CacheLookupOutcome::HitLocal,
        );
    }
    // local miss: try Redis
    let mut miss_outcome: CacheLookupOutcome = CacheLookupOutcome::MissAllTiers;
    if let Some(redis) = GLOBAL_REDIS.get() {
        if should_bypass_redis_temporarily() {
            miss_outcome = CacheLookupOutcome::MissAfterRedisGetTimeout;
        } else {
            let redis_lookup_result =
                tokio::time::timeout(redis_operation_timeout(), redis.get(cache_key)).await;

            match redis_lookup_result {
                Ok(Ok(value)) if !value.is_null() => {
                    note_redis_success();
                    // backfill local cache and return
                    app_state
                        .cache
                        .insert(cache_key.to_string(), value.clone())
                        .await;

                    let serialized_for_fast_path = if let Value::Array(arr) = &value {
                        if arr.len() == 1 {
                            serde_json::to_string(&arr[0]).ok()
                        } else {
                            serde_json::to_string(&value).ok()
                        }
                    } else {
                        serde_json::to_string(&value).ok()
                    };

                    if let Some(raw_body) = serialized_for_fast_path {
                        app_state
                            .cache
                            .insert(raw_key, Value::String(raw_body))
                            .await;
                    }

                    if let Value::Array(arr) = &value
                        && arr.len() == 1
                    {
                        let elapsed_secs = started_at.elapsed().as_secs_f64();
                        app_state.metrics_state.record_management_mutation(
                            lookup_metric,
                            CacheLookupOutcome::HitRedis.as_str(),
                            elapsed_secs,
                        );
                        tracing::debug!(
                            cache_key = %cache_key,
                            source = "redis",
                            duration_ms = started_at.elapsed().as_millis(),
                            "gateway fetch cache hit"
                        );
                        return (
                            Some(HttpResponse::Ok().json(&arr[0])),
                            CacheLookupOutcome::HitRedis,
                        );
                    }
                    let elapsed_secs = started_at.elapsed().as_secs_f64();
                    app_state.metrics_state.record_management_mutation(
                        lookup_metric,
                        CacheLookupOutcome::HitRedis.as_str(),
                        elapsed_secs,
                    );
                    tracing::debug!(
                        cache_key = %cache_key,
                        source = "redis",
                        duration_ms = started_at.elapsed().as_millis(),
                        "gateway fetch cache hit"
                    );
                    return (
                        Some(HttpResponse::Ok().json(value)),
                        CacheLookupOutcome::HitRedis,
                    );
                }
                Ok(Ok(_)) => note_redis_success(),
                Ok(Err(err)) => {
                    note_redis_failure_and_start_cooldown();
                    miss_outcome = CacheLookupOutcome::MissAfterRedisGetError;
                    app_state.metrics_state.record_management_mutation(
                        lookup_metric,
                        "redis_get_error",
                        started_at.elapsed().as_secs_f64(),
                    );
                    tracing::warn!(error = %err, cache_key = %cache_key, "Redis get failed; continuing with local cache only");
                }
                Err(_) => {
                    note_redis_failure_and_start_cooldown();
                    miss_outcome = CacheLookupOutcome::MissAfterRedisGetTimeout;
                    app_state.metrics_state.record_management_mutation(
                        lookup_metric,
                        "redis_get_timeout",
                        started_at.elapsed().as_secs_f64(),
                    );
                    tracing::warn!(cache_key = %cache_key, "Redis get timed out; continuing with local cache only");
                }
            }
        }
    }

    app_state.metrics_state.record_management_mutation(
        lookup_metric,
        CacheLookupOutcome::MissAllTiers.as_str(),
        started_at.elapsed().as_secs_f64(),
    );
    // Keep legacy miss label for compatibility with existing dashboards/tests.
    app_state.metrics_state.record_management_mutation(
        lookup_metric,
        "miss",
        started_at.elapsed().as_secs_f64(),
    );
    (None, miss_outcome)
}

#[cfg(test)]
mod tests {
    use super::{
        CacheLookupOutcome, check_cache_control_and_get_response_v2_with_outcome, raw_cache_key,
    };
    use crate::AppState;
    use crate::api::gateway::insert::{InsertWindowCoordinator, InsertWindowSettings};
    use crate::api::metrics::MetricsState;
    use crate::drivers::postgresql::sqlx_driver::PostgresClientRegistry;
    use actix_web::test::TestRequest;
    use actix_web::web::Data;
    use moka::future::Cache;
    use reqwest::Client;
    use serde_json::{Value, json};
    use std::sync::Arc;
    use std::time::Instant;

    fn test_app_state() -> Data<AppState> {
        let cache: Arc<Cache<String, Value>> = Arc::new(Cache::builder().max_capacity(100).build());
        let immortal_cache: Arc<Cache<String, Value>> =
            Arc::new(Cache::builder().max_capacity(100).build());
        let jdbc_pool_cache = Arc::new(Cache::builder().max_capacity(4).build());

        let insert_window_coordinator: Arc<InsertWindowCoordinator> =
            InsertWindowCoordinator::new(InsertWindowSettings {
                max_batch: 100,
                max_queued: 10_000,
                deny_tables: Default::default(),
            });
        let data: Data<AppState> = Data::new(AppState {
            cache,
            immortal_cache,
            client: Client::new(),
            process_start_time_seconds: 0,
            process_started_at: Instant::now(),
            pg_registry: Arc::new(PostgresClientRegistry::empty()),
            jdbc_pool_cache,
            #[cfg(feature = "deadpool_experimental")]
            deadpool_registry: Arc::new(
                crate::drivers::postgresql::deadpool_registry::DeadpoolPostgresRegistry::empty(),
            ),
            #[cfg(feature = "deadpool_experimental")]
            jdbc_deadpool_cache: Arc::new(Cache::builder().max_capacity(4).build()),
            gateway_force_camel_case_to_snake_case: false,
            gateway_auto_cast_uuid_filter_values_to_text: true,
            gateway_allow_schema_names_prefixed_as_table_name: true,
            pipeline_registry: None,
            logging_client_name: None,
            gateway_auth_client_name: None,
            gateway_api_key_fail_mode: "fail_closed".to_string(),
            gateway_jdbc_allow_private_hosts: false,
            gateway_jdbc_allowed_hosts: Vec::new(),
            gateway_resilience_timeout_secs: 30,
            gateway_resilience_read_max_retries: 1,
            gateway_resilience_initial_backoff_ms: 100,
            gateway_admission_store_backend: "redis".to_string(),
            gateway_admission_store_fail_mode: "fail_closed".to_string(),
            prometheus_metrics_enabled: false,
            metrics_state: Arc::new(MetricsState::new()),
            gateway_insert_execution_window_ms: 0,
            gateway_insert_window_max_batch: 100,
            gateway_insert_window_max_queued: 10_000,
            gateway_insert_merge_deny_tables: Default::default(),
            insert_window_coordinator: insert_window_coordinator.clone(),
        });
        insert_window_coordinator.bind_app_state(data.clone());
        data
    }

    #[tokio::test]
    async fn v2_outcome_bypass_no_cache_header() {
        let state = test_app_state();
        let req = TestRequest::default()
            .insert_header(("Cache-Control", "no-cache"))
            .to_http_request();

        let (response, outcome) = check_cache_control_and_get_response_v2_with_outcome(
            &req,
            state,
            "cache-key",
            "gateway_fetch_cache_lookup",
        )
        .await;

        assert!(response.is_none());
        assert_eq!(outcome, CacheLookupOutcome::BypassNoCacheHeader);
    }

    #[tokio::test]
    async fn v2_outcome_hit_local_raw() {
        let state = test_app_state();
        state
            .cache
            .insert(
                raw_cache_key("cache-key"),
                Value::String("{\"ok\":true}".to_string()),
            )
            .await;

        let req = TestRequest::default().to_http_request();
        let (response, outcome) = check_cache_control_and_get_response_v2_with_outcome(
            &req,
            state,
            "cache-key",
            "gateway_fetch_cache_lookup",
        )
        .await;

        assert!(response.is_some());
        assert_eq!(outcome, CacheLookupOutcome::HitLocalRaw);
    }

    #[tokio::test]
    async fn v2_outcome_hit_local() {
        let state = test_app_state();
        state
            .cache
            .insert(
                "cache-key".to_string(),
                Value::Array(vec![json!({"data": 1})]),
            )
            .await;

        let req = TestRequest::default().to_http_request();
        let (response, outcome) = check_cache_control_and_get_response_v2_with_outcome(
            &req,
            state,
            "cache-key",
            "gateway_fetch_cache_lookup",
        )
        .await;

        assert!(response.is_some());
        assert_eq!(outcome, CacheLookupOutcome::HitLocal);
    }
}