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//!
21//! # Example
22//!
23//! ```rust,no_run
24//! use rust_web_server::app::App;
25//! use rust_web_server::cache::CacheLayer;
26//! use rust_web_server::core::New;
27//!
28//! let app = App::new()
29//!     .wrap(CacheLayer::memory(1000).ttl(60).vary_by_header("Accept"));
30//! ```
31
32#[cfg(test)]
33mod tests;
34
35use std::collections::{HashMap, VecDeque};
36use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
37use std::sync::{Arc, Mutex};
38use std::time::{Duration, Instant};
39
40use crate::application::Application;
41use crate::header::Header;
42use crate::middleware::Middleware;
43use crate::request::{METHOD, Request};
44use crate::response::Response;
45use crate::server::ConnectionInfo;
46
47// ── cache store ───────────────────────────────────────────────────────────────
48
49struct CachedEntry {
50    response: Response,
51    inserted_at: Instant,
52}
53
54struct CacheStore {
55    entries: HashMap<String, CachedEntry>,
56    /// Insertion order — front is oldest; used for capacity eviction.
57    order: VecDeque<String>,
58}
59
60impl CacheStore {
61    fn new() -> Self {
62        CacheStore { entries: HashMap::new(), order: VecDeque::new() }
63    }
64
65    fn get(&self, key: &str, ttl: Duration) -> Option<&CachedEntry> {
66        self.entries.get(key).filter(|e| e.inserted_at.elapsed() < ttl)
67    }
68
69    fn insert(&mut self, key: String, entry: CachedEntry, capacity: usize) {
70        // Update in place without disturbing insertion order.
71        if self.entries.contains_key(&key) {
72            self.entries.insert(key, entry);
73            return;
74        }
75        // `purge_expired` is called by the caller before `insert`, so any
76        // remaining entries are still live. Evict the oldest if we're full.
77        if self.entries.len() >= capacity {
78            if let Some(oldest) = self.order.pop_front() {
79                self.entries.remove(&oldest);
80            }
81        }
82        self.order.push_back(key.clone());
83        self.entries.insert(key, entry);
84    }
85
86    fn purge_expired(&mut self, ttl: Duration) {
87        let expired: Vec<String> = self.entries.iter()
88            .filter(|(_, e)| e.inserted_at.elapsed() >= ttl)
89            .map(|(k, _)| k.clone())
90            .collect();
91        for k in &expired {
92            self.entries.remove(k);
93            self.order.retain(|o| o != k);
94        }
95    }
96}
97
98// ── CacheLayer ────────────────────────────────────────────────────────────────
99
100/// An in-memory response cache middleware.
101///
102/// Construct with [`CacheLayer::memory`] and configure with the builder methods
103/// [`ttl`](CacheLayer::ttl) and [`vary_by_header`](CacheLayer::vary_by_header).
104///
105/// `CacheLayer` is cheaply `Clone`-able — clones share the same underlying
106/// store, which lets you keep a handle for cache stats and invalidation while
107/// the other clone is used as middleware.
108#[derive(Clone)]
109pub struct CacheLayer {
110    store: Arc<Mutex<CacheStore>>,
111    /// `store`'s entry count, kept after every change under its lock: an
112    /// empty cache (nothing cacheable seen yet — e.g. static files, sent
113    /// `no-cache`) costs a request no key and no lock. The lock was shared
114    /// by every worker and taken on every GET.
115    stored: Arc<AtomicUsize>,
116    hits: Arc<AtomicU64>,
117    misses: Arc<AtomicU64>,
118    capacity: usize,
119    ttl: Duration,
120    vary_headers: Vec<String>,
121}
122
123impl CacheLayer {
124    /// Create a new in-memory cache bounded to `capacity` entries.
125    ///
126    /// Default TTL is **60 seconds**. Adjust with [`.ttl()`](CacheLayer::ttl).
127    pub fn memory(capacity: usize) -> Self {
128        CacheLayer {
129            store: Arc::new(Mutex::new(CacheStore::new())),
130            stored: Arc::new(AtomicUsize::new(0)),
131            hits: Arc::new(AtomicU64::new(0)),
132            misses: Arc::new(AtomicU64::new(0)),
133            capacity,
134            ttl: Duration::from_secs(60),
135            vary_headers: vec![],
136        }
137    }
138
139    /// Number of cache hits since the cache was created.
140    pub fn hits(&self) -> u64 {
141        self.hits.load(Ordering::Relaxed)
142    }
143
144    /// Number of cache misses since the cache was created.
145    pub fn misses(&self) -> u64 {
146        self.misses.load(Ordering::Relaxed)
147    }
148
149    /// Current number of entries in the store (including potentially expired ones
150    /// that haven't been purged yet).
151    pub fn size(&self) -> usize {
152        self.store.lock().unwrap().entries.len()
153    }
154
155    /// Remove all entries from the cache.
156    pub fn clear(&self) {
157        let mut guard = self.store.lock().unwrap();
158        guard.entries.clear();
159        guard.order.clear();
160        self.stored.store(0, Ordering::Relaxed);
161    }
162
163    /// Set the time-to-live for cached entries.
164    pub fn ttl(mut self, secs: u64) -> Self {
165        self.ttl = Duration::from_secs(secs);
166        self
167    }
168
169    /// Include a request header in the cache key so that different values of
170    /// that header produce separate cache entries.
171    ///
172    /// Header name matching is case-insensitive. Call multiple times to vary
173    /// by more than one header.
174    ///
175    /// ```rust,no_run
176    /// use rust_web_server::cache::CacheLayer;
177    ///
178    /// let layer = CacheLayer::memory(500)
179    ///     .vary_by_header("Accept")
180    ///     .vary_by_header("Accept-Language");
181    /// ```
182    pub fn vary_by_header(mut self, name: &str) -> Self {
183        self.vary_headers.push(name.to_ascii_lowercase());
184        self
185    }
186
187    fn store(&self) -> &Mutex<CacheStore> {
188        &self.store
189    }
190
191    /// Build a cache key from the request URI and any configured vary headers.
192    fn cache_key(&self, request: &Request) -> String {
193        let mut key = request.request_uri.clone();
194        for vh in &self.vary_headers {
195            let val = request.headers.iter()
196                .find(|h| h.name.eq_ignore_ascii_case(vh))
197                .map(|h| h.value.as_ref())
198                .unwrap_or("");
199            key.push('\x00');
200            key.push_str(val);
201        }
202        key
203    }
204
205    /// `true` when the request carries `Cache-Control: no-cache`, meaning the
206    /// client wants a fresh response (but we may still store the result).
207    fn request_bypasses_cache(request: &Request) -> bool {
208        request.headers.iter().any(|h| {
209            h.name.eq_ignore_ascii_case(Header::_CACHE_CONTROL)
210                && h.value.to_ascii_lowercase().contains("no-cache")
211        })
212    }
213
214    /// `true` for a request whose response may be private to the client
215    /// (credentials, cookies) or partial (`Range`): never answered from, nor
216    /// stored in, the shared cache (#49).
217    fn request_is_private(request: &Request) -> bool {
218        request.headers.iter().any(|h| {
219            h.name.eq_ignore_ascii_case(Header::_AUTHORIZATION)
220                || h.name.eq_ignore_ascii_case(Header::_COOKIE)
221                || h.name.eq_ignore_ascii_case(Header::_RANGE)
222        })
223    }
224
225    /// `true` when the response may be stored and replayed as is:
226    /// - a 2xx to a request without `Range` (a 206 is one slice, not the
227    ///   resource), held in memory (not streamed), without `Set-Cookie`;
228    /// - no `Cache-Control: no-store`, `private` or `no-cache` (`no-cache`
229    ///   allows storing only to revalidate before every reuse, which this
230    ///   cache can't do — static files are sent with it by default);
231    /// - every request header its `Vary` names is part of the cache key, or
232    ///   didn't change this response: `Origin` without an
233    ///   `Access-Control-Allow-Origin` in it, `Accept-Encoding` with an
234    ///   unencoded body (valid for every client). Otherwise e.g. a gzipped
235    ///   body would be replayed to a client that didn't ask for gzip.
236    ///   `Vary: *` is never cacheable.
237    fn response_is_cacheable(&self, request: &Request, response: &Response) -> bool {
238        if response.status_code < 200 || response.status_code >= 300 {
239            return false;
240        }
241        if request.header(Header::_RANGE).is_some() {
242            return false;
243        }
244        // A streamed body isn't held in the response: a stored copy would
245        // replay an empty body. A cookie-setting response is per client (#49).
246        if response.stream_file.is_some() || response.stream_pipe.is_some() {
247            return false;
248        }
249        if response.headers.iter().any(|h| h.name.eq_ignore_ascii_case(Header::_SET_COOKIE)) {
250            return false;
251        }
252        let has = |name: &str| response.headers.iter().any(|h| h.name.eq_ignore_ascii_case(name));
253        let varies_harmlessly = |name: &str| {
254            (name.eq_ignore_ascii_case(Header::_ORIGIN) && !has(Header::_ACCESS_CONTROL_ALLOW_ORIGIN))
255                || (name.eq_ignore_ascii_case(Header::_ACCEPT_ENCODING) && !has(Header::_CONTENT_ENCODING))
256        };
257        response.headers.iter().all(|h| {
258            if h.name.eq_ignore_ascii_case(Header::_CACHE_CONTROL) {
259                let v = h.value.to_ascii_lowercase();
260                !(v.contains("no-store") || v.contains("private") || v.contains("no-cache"))
261            } else if h.name.eq_ignore_ascii_case(Header::_VARY) {
262                h.value.split(',').map(str::trim).filter(|name| !name.is_empty()).all(|name| {
263                    name != "*" && (self.vary_headers.iter().any(|vh| vh.eq_ignore_ascii_case(name)) || varies_harmlessly(name))
264                })
265            } else {
266                true
267            }
268        })
269    }
270
271    /// Age of the entry in whole seconds, capped at u64::MAX.
272    fn age_secs(entry: &CachedEntry) -> u64 {
273        entry.inserted_at.elapsed().as_secs()
274    }
275
276    /// Build a response from a cache hit, injecting an `Age` header.
277    fn cached_response(entry: &CachedEntry) -> Response {
278        let mut response = entry.response.clone();
279        let age = Self::age_secs(entry);
280        // Replace or add the Age header.
281        if let Some(h) = response.headers.iter_mut().find(|h| h.name.eq_ignore_ascii_case("Age")) {
282            h.value = age.to_string().into();
283        } else {
284            response.headers.push(Header { name: "Age".into(), value: age.to_string().into() });
285        }
286        response
287    }
288}
289
290impl Middleware for CacheLayer {
291    /// Headers only: a streamed body passes through untouched.
292    fn accepts_streamed_body(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
293        true
294    }
295
296
297    fn handle(
298        &self,
299        request: &Request,
300        connection: &ConnectionInfo,
301        next: &dyn Application,
302    ) -> Result<Response, String> {
303        // Only cache GET requests, and never private or partial ones.
304        if request.method != METHOD.get || Self::request_is_private(request) {
305            return next.execute(request, connection);
306        }
307
308        let bypass = Self::request_bypasses_cache(request);
309
310        if !bypass && self.stored.load(Ordering::Relaxed) > 0 {
311            // Check for a valid cache hit.
312            let key = self.cache_key(request);
313            let guard = self.store().lock().unwrap();
314            if let Some(entry) = guard.get(&key, self.ttl) {
315                self.hits.fetch_add(1, Ordering::Relaxed);
316                return Ok(Self::cached_response(entry));
317            }
318        }
319        self.misses.fetch_add(1, Ordering::Relaxed);
320
321        // Cache miss (or bypass): call the handler.
322        let response = next.execute(request, connection)?;
323
324        if self.response_is_cacheable(request, &response) {
325            let key = self.cache_key(request);
326            let mut guard = self.store().lock().unwrap();
327            guard.purge_expired(self.ttl);
328            guard.insert(
329                key,
330                CachedEntry { response: response.clone(), inserted_at: Instant::now() },
331                self.capacity,
332            );
333            self.stored.store(guard.entries.len(), Ordering::Relaxed);
334        }
335
336        Ok(response)
337    }
338
339    /// In-memory only: safe on the event loop.
340    fn runs_inline(&self, _request: &Request, _connection: &ConnectionInfo) -> bool {
341        true
342    }
343}