Skip to main content

rust_web_server/cache/
mod.rs

1//! In-memory response cache middleware.
2//!
3//! [`CacheLayer`] implements [`Middleware`] and short-circuits the inner
4//! application for cacheable `GET` responses within their TTL.  Entries are
5//! bounded by a configurable capacity; the oldest entry is evicted when the
6//! store is full and no expired entries remain.
7//!
8//! # What is cached
9//!
10//! - Method: **GET only**; all other methods bypass the cache.
11//! - Status: 2xx responses (200, 201, 203, 204, 206, …).
12//! - Response `Cache-Control: no-store` or `private` — **not** cached.
13//! - Private or partial requests — `Authorization`, `Cookie` or `Range` — skip
14//!   the cache entirely (no lookup, no store): their responses may differ per
15//!   user or cover only part of the resource.
16//! - Responses that set a cookie (`Set-Cookie`) or stream their body
17//!   (`stream_file` / `stream_pipe`, not held in memory) are **not** cached.
18//! - Request `Cache-Control: no-cache` — cache is bypassed, handler is called,
19//!   but the fresh response **is** stored (revalidation).
20//! - How long: the response's `Cache-Control: s-maxage`, else `max-age`,
21//!   capped by the layer's [`ttl`](CacheLayer::ttl); the `ttl` when it says
22//!   neither. `max-age=0` is not stored (since 18.11.0; before, every
23//!   response was kept for the `ttl`). Hits carry an `Age` header.
24//!
25//! # Example
26//!
27//! ```rust,no_run
28//! use rust_web_server::app::App;
29//! use rust_web_server::cache::CacheLayer;
30//! use rust_web_server::core::New;
31//!
32//! let app = App::new()
33//!     .wrap(CacheLayer::memory(1000).ttl(60).vary_by_header("Accept"));
34//! ```
35
36#[cfg(test)]
37mod tests;
38#[cfg(not(target_arch = "wasm32"))]
39pub(crate) mod disk;
40
41use std::collections::{HashMap, VecDeque};
42use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
43use std::sync::{Arc, Mutex};
44use std::time::{Duration, Instant};
45
46use crate::application::Application;
47use crate::header::Header;
48use crate::middleware::Middleware;
49use crate::request::{METHOD, Request};
50use crate::response::Response;
51use crate::server::ConnectionInfo;
52
53// ── cache store ───────────────────────────────────────────────────────────────
54
55struct CachedEntry {
56    response: Response,
57    inserted_at: Instant,
58    /// How long this entry is fresh: the layer's `ttl`, or less when the
59    /// response's `Cache-Control` said so.
60    ttl: Duration,
61}
62
63struct CacheStore {
64    entries: HashMap<String, CachedEntry>,
65    /// Insertion order — front is oldest; used for capacity eviction.
66    order: VecDeque<String>,
67}
68
69impl CacheStore {
70    fn new() -> Self {
71        CacheStore { entries: HashMap::new(), order: VecDeque::new() }
72    }
73
74    fn get(&self, key: &str) -> Option<&CachedEntry> {
75        self.entries.get(key).filter(|e| e.inserted_at.elapsed() < e.ttl)
76    }
77
78    fn insert(&mut self, key: String, entry: CachedEntry, capacity: usize) {
79        // Update in place without disturbing insertion order.
80        if self.entries.contains_key(&key) {
81            self.entries.insert(key, entry);
82            return;
83        }
84        // `purge_expired` is called by the caller before `insert`, so any
85        // remaining entries are still live. Evict the oldest if we're full.
86        if self.entries.len() >= capacity {
87            if let Some(oldest) = self.order.pop_front() {
88                self.entries.remove(&oldest);
89            }
90        }
91        self.order.push_back(key.clone());
92        self.entries.insert(key, entry);
93    }
94
95    fn purge_expired(&mut self) {
96        let expired: Vec<String> = self.entries.iter()
97            .filter(|(_, e)| e.inserted_at.elapsed() >= e.ttl)
98            .map(|(k, _)| k.clone())
99            .collect();
100        for k in &expired {
101            self.entries.remove(k);
102            self.order.retain(|o| o != k);
103        }
104    }
105}
106
107/// How long `response` may be served from a shared cache: its
108/// `Cache-Control: s-maxage`, else `max-age` (RFC 9111 §5.2.2), capped by
109/// `cap`, the layer's `ttl`; `cap` when it says neither. Zero: don't store
110/// (`max-age=0`). A directive whose value isn't a number is ignored.
111pub(crate) fn freshness(response: &Response, cap: Duration) -> Duration {
112    let mut s_maxage = None;
113    let mut max_age = None;
114    for h in response.headers.iter().filter(|h| h.name.eq_ignore_ascii_case(Header::_CACHE_CONTROL)) {
115        for directive in h.value.split(',') {
116            let Some((name, value)) = directive.split_once('=') else { continue };
117            let Ok(secs) = value.trim().trim_matches('"').parse::<u64>() else { continue };
118            match name.trim().to_ascii_lowercase().as_str() {
119                "s-maxage" => s_maxage = Some(s_maxage.map_or(secs, |v: u64| v.min(secs))),
120                "max-age" => max_age = Some(max_age.map_or(secs, |v: u64| v.min(secs))),
121                _ => {}
122            }
123        }
124    }
125    match s_maxage.or(max_age) {
126        Some(secs) => cap.min(Duration::from_secs(secs)),
127        None => cap,
128    }
129}
130
131// ── purge ─────────────────────────────────────────────────────────────────────
132
133/// What [`CacheLayer::purge`] removes. A cache key is the request target
134/// (path and query) plus the values of `vary_by_header`s, so `Exact` removes
135/// every variant of one target.
136#[derive(Debug, Clone, PartialEq, Eq)]
137pub enum Purge {
138    /// Everything.
139    All,
140    /// One request target, e.g. `/products/42` or `/search?q=lamp`.
141    Exact(String),
142    /// Every target starting with this, e.g. `/products/`.
143    Prefix(String),
144}
145
146impl Purge {
147    fn matches(&self, key: &str) -> bool {
148        let target = key.split('\x00').next().unwrap_or(key);
149        match self {
150            Purge::All => true,
151            Purge::Exact(t) => target == t,
152            Purge::Prefix(p) => target.starts_with(p.as_str()),
153        }
154    }
155
156    /// A `PURGE` request's target: a trailing `*` makes it a prefix.
157    pub fn from_target(target: &str) -> Purge {
158        match target.strip_suffix('*') {
159            Some(prefix) => Purge::Prefix(prefix.to_string()),
160            None => Purge::Exact(target.to_string()),
161        }
162    }
163}
164
165/// Equal, in time that depends only on the lengths.
166fn same_bytes(a: &[u8], b: &[u8]) -> bool {
167    a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
168}
169
170/// Named caches for the admin API: the binary's own and one per
171/// config-file route that has `[route.middleware.cache]`.
172pub mod registry {
173    use super::CacheLayer;
174    use std::collections::BTreeMap;
175    use std::sync::{Mutex, OnceLock};
176
177    fn caches() -> &'static Mutex<BTreeMap<String, CacheLayer>> {
178        static CACHES: OnceLock<Mutex<BTreeMap<String, CacheLayer>>> = OnceLock::new();
179        CACHES.get_or_init(Mutex::default)
180    }
181
182    /// Make `cache` reachable as `name` (a clone: it shares the store).
183    pub fn register(name: impl Into<String>, cache: CacheLayer) {
184        caches().lock().unwrap_or_else(|e| e.into_inner()).insert(name.into(), cache);
185    }
186
187    /// Replace every cache whose name starts with `group` by `caches` (a new
188    /// config generation's routes).
189    pub(crate) fn replace_group(group: &str, new: Vec<(String, CacheLayer)>) {
190        let mut map = caches().lock().unwrap_or_else(|e| e.into_inner());
191        map.retain(|name, _| !name.starts_with(group));
192        map.extend(new);
193    }
194
195    /// Every registered cache, by name.
196    pub fn list() -> Vec<(String, CacheLayer)> {
197        caches().lock().unwrap_or_else(|e| e.into_inner()).iter().map(|(n, c)| (n.clone(), c.clone())).collect()
198    }
199
200    pub fn get(name: &str) -> Option<CacheLayer> {
201        caches().lock().unwrap_or_else(|e| e.into_inner()).get(name).cloned()
202    }
203}
204
205// ── CacheLayer ────────────────────────────────────────────────────────────────
206
207/// An in-memory response cache middleware.
208///
209/// Construct with [`CacheLayer::memory`] and configure with the builder methods
210/// [`ttl`](CacheLayer::ttl) and [`vary_by_header`](CacheLayer::vary_by_header).
211///
212/// `CacheLayer` is cheaply `Clone`-able — clones share the same underlying
213/// store, which lets you keep a handle for cache stats and invalidation while
214/// the other clone is used as middleware.
215#[derive(Clone)]
216pub struct CacheLayer {
217    store: Arc<Mutex<CacheStore>>,
218    /// `store`'s entry count, kept after every change under its lock: an
219    /// empty cache (nothing cacheable seen yet — e.g. static files, sent
220    /// `no-cache`) costs a request no key and no lock. The lock was shared
221    /// by every worker and taken on every GET.
222    stored: Arc<AtomicUsize>,
223    hits: Arc<AtomicU64>,
224    misses: Arc<AtomicU64>,
225    capacity: usize,
226    ttl: Duration,
227    vary_headers: Vec<String>,
228    /// [`CacheLayer::disk`]: entries in files instead of `store`.
229    #[cfg(not(target_arch = "wasm32"))]
230    disk: Option<Arc<disk::DiskStore>>,
231    /// [`CacheLayer::purge_token`]: accept `PURGE` with this bearer token.
232    purge_token: Option<Arc<String>>,
233}
234
235impl CacheLayer {
236    /// Create a new in-memory cache bounded to `capacity` entries.
237    ///
238    /// Default TTL is **60 seconds**. Adjust with [`.ttl()`](CacheLayer::ttl).
239    pub fn memory(capacity: usize) -> Self {
240        CacheLayer {
241            store: Arc::new(Mutex::new(CacheStore::new())),
242            stored: Arc::new(AtomicUsize::new(0)),
243            hits: Arc::new(AtomicU64::new(0)),
244            misses: Arc::new(AtomicU64::new(0)),
245            capacity,
246            ttl: Duration::from_secs(60),
247            vary_headers: vec![],
248            #[cfg(not(target_arch = "wasm32"))]
249            disk: None,
250            purge_token: None,
251        }
252    }
253
254    /// Accept the HTTP `PURGE` method on the routes behind this layer,
255    /// authenticated by `Authorization: Bearer <token>` (since 18.11.0).
256    /// `PURGE /products/42` removes that target (every `Vary` variant);
257    /// `PURGE /products/*` removes the prefix. The answer is
258    /// `{"purged":<n>}`; a missing or wrong token gets `401`. Without a
259    /// token, `PURGE` passes through to the application as before.
260    pub fn purge_token(mut self, token: impl Into<String>) -> Self {
261        let token = token.into();
262        self.purge_token = (!token.is_empty()).then(|| Arc::new(token));
263        self
264    }
265
266    /// Remove the entries `target` names; how many.
267    pub fn purge(&self, target: &Purge) -> usize {
268        if let Some(disk) = self.disk_store() {
269            let n = disk.remove_where(|key| target.matches(key));
270            self.stored.store(disk.len(), Ordering::Relaxed);
271            return n;
272        }
273        let mut guard = self.store.lock().unwrap_or_else(|e| e.into_inner());
274        let keys: Vec<String> = guard.entries.keys().filter(|k| target.matches(k)).cloned().collect();
275        for key in &keys {
276            guard.entries.remove(key);
277        }
278        guard.order.retain(|k| !keys.contains(k));
279        self.stored.store(guard.entries.len(), Ordering::Relaxed);
280        keys.len()
281    }
282
283    /// `"memory"` or `"disk"`.
284    pub fn kind(&self) -> &'static str {
285        if self.disk_store().is_some() { "disk" } else { "memory" }
286    }
287
288    /// The `PURGE` answer, when this request is one and a token is set.
289    fn answer_purge(&self, request: &Request) -> Option<Response> {
290        let token = self.purge_token.as_ref()?;
291        if !request.method.eq_ignore_ascii_case("PURGE") {
292            return None;
293        }
294        use crate::response::STATUS_CODE_REASON_PHRASE;
295        let sent = request.header(Header::_AUTHORIZATION).and_then(|h| h.value.strip_prefix("Bearer ").map(str::trim));
296        let authorized = sent.is_some_and(|s| same_bytes(s.as_bytes(), token.as_bytes()));
297        if !authorized {
298            let mut response = Response::json(STATUS_CODE_REASON_PHRASE.n401_unauthorized, br#"{"error":"PURGE needs the purge token"}"#.to_vec());
299            response.headers.push(Header::new("WWW-Authenticate", r#"Bearer realm="cache purge""#));
300            return Some(response);
301        }
302        let n = self.purge(&Purge::from_target(&request.request_uri));
303        Some(Response::json(STATUS_CODE_REASON_PHRASE.n200_ok, format!(r#"{{"purged":{n}}}"#).into_bytes()))
304    }
305
306    /// A cache kept in files under `dir` (created if missing), bounded to
307    /// `max_bytes` in total, oldest dropped first (since 18.11.0). Entries
308    /// survive a restart: the directory is indexed when the layer is built,
309    /// expired and unreadable files removed. Same rules as
310    /// [`memory`](CacheLayer::memory) for what is cached and for how long.
311    ///
312    /// ```rust,no_run
313    /// use rust_web_server::cache::CacheLayer;
314    ///
315    /// let layer = CacheLayer::disk("/var/cache/rws/api", 512 * 1024 * 1024)?.ttl(600);
316    /// # Ok::<(), std::io::Error>(())
317    /// ```
318    #[cfg(not(target_arch = "wasm32"))]
319    pub fn disk(dir: impl Into<std::path::PathBuf>, max_bytes: u64) -> std::io::Result<Self> {
320        let store = disk::DiskStore::open(dir, max_bytes)?;
321        let mut layer = CacheLayer::memory(0);
322        layer.stored.store(store.len(), Ordering::Relaxed);
323        layer.disk = Some(Arc::new(store));
324        Ok(layer)
325    }
326
327    /// The disk store, when this layer has one.
328    #[cfg(not(target_arch = "wasm32"))]
329    fn disk_store(&self) -> Option<&disk::DiskStore> {
330        self.disk.as_deref()
331    }
332
333    #[cfg(target_arch = "wasm32")]
334    fn disk_store(&self) -> Option<&NoDisk> {
335        None
336    }
337
338    /// Number of cache hits since the cache was created.
339    pub fn hits(&self) -> u64 {
340        self.hits.load(Ordering::Relaxed)
341    }
342
343    /// Number of cache misses since the cache was created.
344    pub fn misses(&self) -> u64 {
345        self.misses.load(Ordering::Relaxed)
346    }
347
348    /// Current number of entries in the store (including potentially expired ones
349    /// that haven't been purged yet).
350    pub fn size(&self) -> usize {
351        if let Some(disk) = self.disk_store() {
352            return disk.len();
353        }
354        self.store.lock().unwrap().entries.len()
355    }
356
357    /// Bytes held by a disk cache's files; `None` for a memory cache.
358    pub fn stored_bytes(&self) -> Option<u64> {
359        #[cfg(not(target_arch = "wasm32"))]
360        return self.disk_store().map(disk::DiskStore::bytes);
361        #[cfg(target_arch = "wasm32")]
362        None
363    }
364
365    /// Remove all entries from the cache.
366    pub fn clear(&self) {
367        if let Some(disk) = self.disk_store() {
368            disk.remove_where(|_| true);
369            self.stored.store(0, Ordering::Relaxed);
370            return;
371        }
372        let mut guard = self.store.lock().unwrap();
373        guard.entries.clear();
374        guard.order.clear();
375        self.stored.store(0, Ordering::Relaxed);
376    }
377
378    /// Set the time-to-live for cached entries.
379    pub fn ttl(mut self, secs: u64) -> Self {
380        self.ttl = Duration::from_secs(secs);
381        self
382    }
383
384    /// Include a request header in the cache key so that different values of
385    /// that header produce separate cache entries.
386    ///
387    /// Header name matching is case-insensitive. Call multiple times to vary
388    /// by more than one header.
389    ///
390    /// ```rust,no_run
391    /// use rust_web_server::cache::CacheLayer;
392    ///
393    /// let layer = CacheLayer::memory(500)
394    ///     .vary_by_header("Accept")
395    ///     .vary_by_header("Accept-Language");
396    /// ```
397    pub fn vary_by_header(mut self, name: &str) -> Self {
398        self.vary_headers.push(name.to_ascii_lowercase());
399        self
400    }
401
402    fn store(&self) -> &Mutex<CacheStore> {
403        &self.store
404    }
405
406    /// Build a cache key from the request URI and any configured vary headers.
407    fn cache_key(&self, request: &Request) -> String {
408        let mut key = request.request_uri.clone();
409        for vh in &self.vary_headers {
410            let val = request.headers.iter()
411                .find(|h| h.name.eq_ignore_ascii_case(vh))
412                .map(|h| h.value.as_ref())
413                .unwrap_or("");
414            key.push('\x00');
415            key.push_str(val);
416        }
417        key
418    }
419
420    /// `true` when the request carries `Cache-Control: no-cache`, meaning the
421    /// client wants a fresh response (but we may still store the result).
422    fn request_bypasses_cache(request: &Request) -> bool {
423        request.headers.iter().any(|h| {
424            h.name.eq_ignore_ascii_case(Header::_CACHE_CONTROL)
425                && h.value.to_ascii_lowercase().contains("no-cache")
426        })
427    }
428
429    /// `true` for a request whose response may be private to the client
430    /// (credentials, cookies) or partial (`Range`): never answered from, nor
431    /// stored in, the shared cache (#49).
432    fn request_is_private(request: &Request) -> bool {
433        request.headers.iter().any(|h| {
434            h.name.eq_ignore_ascii_case(Header::_AUTHORIZATION)
435                || h.name.eq_ignore_ascii_case(Header::_COOKIE)
436                || h.name.eq_ignore_ascii_case(Header::_RANGE)
437        })
438    }
439
440    /// `true` when the response may be stored and replayed as is:
441    /// - a 2xx to a request without `Range` (a 206 is one slice, not the
442    ///   resource), held in memory (not streamed), without `Set-Cookie`;
443    /// - no `Cache-Control: no-store`, `private` or `no-cache` (`no-cache`
444    ///   allows storing only to revalidate before every reuse, which this
445    ///   cache can't do — static files are sent with it by default);
446    /// - every request header its `Vary` names is part of the cache key, or
447    ///   didn't change this response: `Origin` without an
448    ///   `Access-Control-Allow-Origin` in it, `Accept-Encoding` with an
449    ///   unencoded body (valid for every client). Otherwise e.g. a gzipped
450    ///   body would be replayed to a client that didn't ask for gzip.
451    ///   `Vary: *` is never cacheable.
452    fn response_is_cacheable(&self, request: &Request, response: &Response) -> bool {
453        if response.status_code < 200 || response.status_code >= 300 {
454            return false;
455        }
456        if request.header(Header::_RANGE).is_some() {
457            return false;
458        }
459        // A streamed body isn't held in the response: a stored copy would
460        // replay an empty body. A cookie-setting response is per client (#49).
461        if response.stream_file.is_some() || response.stream_pipe.is_some() {
462            return false;
463        }
464        if response.headers.iter().any(|h| h.name.eq_ignore_ascii_case(Header::_SET_COOKIE)) {
465            return false;
466        }
467        let has = |name: &str| response.headers.iter().any(|h| h.name.eq_ignore_ascii_case(name));
468        let varies_harmlessly = |name: &str| {
469            (name.eq_ignore_ascii_case(Header::_ORIGIN) && !has(Header::_ACCESS_CONTROL_ALLOW_ORIGIN))
470                || (name.eq_ignore_ascii_case(Header::_ACCEPT_ENCODING) && !has(Header::_CONTENT_ENCODING))
471        };
472        response.headers.iter().all(|h| {
473            if h.name.eq_ignore_ascii_case(Header::_CACHE_CONTROL) {
474                let v = h.value.to_ascii_lowercase();
475                !(v.contains("no-store") || v.contains("private") || v.contains("no-cache"))
476            } else if h.name.eq_ignore_ascii_case(Header::_VARY) {
477                h.value.split(',').map(str::trim).filter(|name| !name.is_empty()).all(|name| {
478                    name != "*" && (self.vary_headers.iter().any(|vh| vh.eq_ignore_ascii_case(name)) || varies_harmlessly(name))
479                })
480            } else {
481                true
482            }
483        })
484    }
485
486    /// Age of the entry in whole seconds, capped at u64::MAX.
487    fn age_secs(entry: &CachedEntry) -> u64 {
488        entry.inserted_at.elapsed().as_secs()
489    }
490
491    /// Build a response from a cache hit, injecting an `Age` header.
492    fn cached_response(entry: &CachedEntry) -> Response {
493        let mut response = entry.response.clone();
494        let age = Self::age_secs(entry);
495        // Replace or add the Age header.
496        if let Some(h) = response.headers.iter_mut().find(|h| h.name.eq_ignore_ascii_case("Age")) {
497            h.value = age.to_string().into();
498        } else {
499            response.headers.push(Header { name: "Age".into(), value: age.to_string().into() });
500        }
501        response
502    }
503}
504
505impl Middleware for CacheLayer {
506    /// Headers only: a streamed body passes through untouched.
507    fn accepts_streamed_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
508        true
509    }
510
511
512    fn handle(
513        &self,
514        request: &Request,
515        connection: &ConnectionInfo,
516        next: &dyn Application,
517    ) -> Result<Response, String> {
518        if let Some(answer) = self.answer_purge(request) {
519            return Ok(answer);
520        }
521        // Only cache GET requests, and never private or partial ones.
522        if request.method != METHOD.get || Self::request_is_private(request) {
523            return next.execute(request, connection);
524        }
525
526        let bypass = Self::request_bypasses_cache(request);
527
528        if let Some(disk) = self.disk_store() {
529            if !bypass && !disk.is_empty() {
530                let key = self.cache_key(request);
531                if let Some((mut response, age)) = disk.get(&key) {
532                    self.hits.fetch_add(1, Ordering::Relaxed);
533                    response.headers.retain(|h| !h.name.eq_ignore_ascii_case("Age"));
534                    response.headers.push(Header { name: "Age".into(), value: age.as_secs().to_string().into() });
535                    return Ok(response);
536                }
537            }
538            self.misses.fetch_add(1, Ordering::Relaxed);
539            let response = next.execute(request, connection)?;
540            let ttl = freshness(&response, self.ttl);
541            if !ttl.is_zero() && self.response_is_cacheable(request, &response) {
542                disk.put(&self.cache_key(request), &response, ttl);
543                self.stored.store(disk.len(), Ordering::Relaxed);
544            }
545            return Ok(response);
546        }
547
548        if !bypass && self.stored.load(Ordering::Relaxed) > 0 {
549            // Check for a valid cache hit.
550            let key = self.cache_key(request);
551            let guard = self.store().lock().unwrap();
552            if let Some(entry) = guard.get(&key) {
553                self.hits.fetch_add(1, Ordering::Relaxed);
554                return Ok(Self::cached_response(entry));
555            }
556        }
557        self.misses.fetch_add(1, Ordering::Relaxed);
558
559        // Cache miss (or bypass): call the handler.
560        let response = next.execute(request, connection)?;
561
562        let ttl = freshness(&response, self.ttl);
563        if !ttl.is_zero() && self.response_is_cacheable(request, &response) {
564            let key = self.cache_key(request);
565            let mut guard = self.store().lock().unwrap();
566            guard.purge_expired();
567            guard.insert(
568                key,
569                CachedEntry { response: response.clone(), inserted_at: Instant::now(), ttl },
570                self.capacity,
571            );
572            self.stored.store(guard.entries.len(), Ordering::Relaxed);
573        }
574
575        Ok(response)
576    }
577
578    /// In-memory: safe on the event loop. A disk store reads and writes
579    /// files, so it runs on the blocking pool.
580    fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
581        self.disk_store().is_none()
582    }
583}
584
585/// Stand-in for the disk store where there is none (wasm32).
586#[cfg(target_arch = "wasm32")]
587struct NoDisk;
588
589#[cfg(target_arch = "wasm32")]
590impl NoDisk {
591    fn len(&self) -> usize { 0 }
592    fn is_empty(&self) -> bool { true }
593    fn remove_where(&self, _matches: impl Fn(&str) -> bool) -> usize { 0 }
594    fn get(&self, _key: &str) -> Option<(Response, Duration)> { None }
595    fn put(&self, _key: &str, _response: &Response, _ttl: Duration) {}
596}