Skip to main content

umbral_cache/
lib.rs

1//! umbral-cache — pluggable cache for umbral.
2//!
3//! A cache framework, the slice that matters for production:
4//! a [`Cache`] handle over a [`CacheBackend`] trait, three built-in
5//! backends (in-memory, SQLite, Redis), and a [`cache_page`] view
6//! middleware that caches full GET responses.
7//!
8//! ```ignore
9//! // Boot wiring (App::builder)
10//! let cache = Cache::memory();
11//! // … or for Redis in production:
12//! // let cache = Cache::redis("redis://localhost:6379/0").await?;
13//! CachePlugin::init(cache.clone());
14//!
15//! // In a handler — explicit cache access
16//! cache.set("homepage:html", &rendered, Some(Duration::from_secs(60))).await;
17//! if let Some(html) = cache.get::<String>("homepage:html").await {
18//!     return Ok(Html(html));
19//! }
20//!
21//! // View-level caching (wraps a Router subtree)
22//! use umbral_cache::cache_page;
23//! let public = Router::new()
24//!     .route("/", get(home))
25//!     .layer(cache_page(Duration::from_secs(60)));
26//! ```
27//!
28//! ## Surface
29//!
30//! - [`CacheBackend`] — the trait. Bytes in, bytes out, async.
31//! - [`CacheError`] — unified error type for backends that can fail.
32//! - [`Cache`] — the handle. Generic-over-T methods wrap the backend
33//!   with serde encoding so callers traffic in their own types.
34//! - [`MemoryBackend`] — `tokio::sync::Mutex<HashMap>` with per-key
35//!   expiry. Lost on process exit. Default choice for development
36//!   and single-process deployments.
37//! - [`SqliteBackend`] — table-backed, durable across restarts.
38//!   Expired rows are lazily skipped on read and cleared on a
39//!   background pass when [`SqliteBackend::sweep`] is called.
40//! - [`RedisBackend`] — (feature = `"redis"`) production backend via
41//!   `redis::aio::ConnectionManager`. Handles reconnect transparently.
42//! - [`cache_page`] — tower [`Layer`] that caches full GET/HEAD responses.
43//!   Only status 200 is cached; skips when `Cache-Control: no-store`
44//!   or `Set-Cookie` appears on the response.
45//! - [`CachePlugin`] — empty Plugin impl so other plugins can name
46//!   "cache" as a dependency.
47//!
48//! ## Deferred past v0
49//!
50//! - `get_or_set` helper that fills on miss inside a single round-trip.
51//! - Versioned keys + `incr/decr` atomic ops.
52//! - Memcached backend.
53//! - Distributed cache invalidation (tag-based).
54//! - ETag / 304 conditional caching inside `cache_page` — the current
55//!   implementation always serves the cached body in full.
56
57use std::collections::HashMap;
58use std::sync::{Arc, OnceLock};
59use std::time::Duration;
60
61use async_trait::async_trait;
62use chrono::{DateTime, Utc};
63use http::header::{CACHE_CONTROL, HeaderValue, VARY};
64use serde::{Serialize, de::DeserializeOwned};
65use sqlx::SqlitePool;
66use tokio::sync::Mutex;
67use tower_http::compression::CompressionLayer;
68use tower_http::set_header::SetResponseHeaderLayer;
69use umbral::prelude::*;
70
71pub mod cache_page;
72pub use cache_page::cache_page;
73
74// ── Ambient cache handle ─────────────────────────────────────────────────────
75
76/// Process-wide ambient cache, set once during `App::build()` (or manually
77/// by calling [`CachePlugin::init`]). `cache_page` reads this automatically.
78static AMBIENT_CACHE: OnceLock<Cache> = OnceLock::new();
79
80/// Return the ambient cache, or `None` if [`CachePlugin::init`] hasn't run.
81pub fn ambient() -> Option<&'static Cache> {
82    AMBIENT_CACHE.get()
83}
84
85// ── Error type ───────────────────────────────────────────────────────────────
86
87/// Error variants emitted by cache backends that can fail (Redis, SQLite).
88/// `MemoryBackend` is infallible — its methods are fire-and-forget.
89#[derive(Debug)]
90pub enum CacheError {
91    /// A Redis-level error (connection, protocol, server).
92    #[cfg(feature = "redis")]
93    Redis(redis::RedisError),
94    /// A SQLite-level error.
95    Sqlx(sqlx::Error),
96    /// Any other I/O or configuration error.
97    Other(String),
98}
99
100impl std::fmt::Display for CacheError {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        match self {
103            #[cfg(feature = "redis")]
104            CacheError::Redis(e) => write!(f, "cache redis error: {e}"),
105            CacheError::Sqlx(e) => write!(f, "cache sqlite error: {e}"),
106            CacheError::Other(s) => write!(f, "cache error: {s}"),
107        }
108    }
109}
110
111impl std::error::Error for CacheError {}
112
113#[cfg(feature = "redis")]
114impl From<redis::RedisError> for CacheError {
115    fn from(e: redis::RedisError) -> Self {
116        CacheError::Redis(e)
117    }
118}
119
120impl From<sqlx::Error> for CacheError {
121    fn from(e: sqlx::Error) -> Self {
122        CacheError::Sqlx(e)
123    }
124}
125
126// ── CacheBackend trait ───────────────────────────────────────────────────────
127
128/// Bytes-in / bytes-out backend. All methods are async because the
129/// SQLite and Redis implementations need to be.
130///
131/// `get_bytes` / `set_bytes` / `delete` / `clear` are infallible at the
132/// trait level — backends swallow errors internally and log them rather
133/// than propagating. Constructors (`new`, `connect`) surface errors via
134/// [`CacheError`] so misconfiguration is caught at boot.
135#[async_trait]
136pub trait CacheBackend: Send + Sync {
137    async fn get(&self, key: &str) -> Option<Vec<u8>>;
138    async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>);
139    async fn delete(&self, key: &str);
140    async fn clear(&self);
141}
142
143// ── Cache handle ─────────────────────────────────────────────────────────────
144
145/// Public handle. Owns its backend behind an Arc so views can clone
146/// it freely (typically stashed in the request context or accessed via
147/// the ambient [`AMBIENT_CACHE`]).
148#[derive(Clone)]
149pub struct Cache {
150    backend: Arc<dyn CacheBackend>,
151}
152
153impl Cache {
154    /// Build a cache backed by a freshly-allocated [`MemoryBackend`].
155    pub fn memory() -> Self {
156        Self {
157            backend: Arc::new(MemoryBackend::default()),
158        }
159    }
160
161    /// Build a cache backed by a SQLite table. The constructor
162    /// creates the table on first call; it's idempotent.
163    pub async fn sqlite(pool: SqlitePool) -> Result<Self, CacheError> {
164        let backend = SqliteBackend::new(pool).await?;
165        Ok(Self {
166            backend: Arc::new(backend),
167        })
168    }
169
170    /// Build a cache backed by Redis.
171    ///
172    /// `url` is a Redis connection string: `redis://[user:pass@]host:port/[db]`.
173    /// Examples: `redis://localhost:6379/0`, `redis://:password@redis.example.com:6379`.
174    ///
175    /// The underlying [`redis::aio::ConnectionManager`] reconnects automatically
176    /// on dropped connections so the handle is safe to clone and reuse for the
177    /// lifetime of the process.
178    #[cfg(feature = "redis")]
179    pub async fn redis(url: &str) -> Result<Self, CacheError> {
180        let backend = RedisBackend::connect(url).await?;
181        Ok(Self {
182            backend: Arc::new(backend),
183        })
184    }
185
186    /// Wrap an arbitrary backend.
187    pub fn with_backend(backend: Arc<dyn CacheBackend>) -> Self {
188        Self { backend }
189    }
190
191    /// Look up a key, deserialise to T. Returns None on miss, on
192    /// expiry, or on a decode error (the entry is treated as
193    /// poisoned and ignored rather than crashing the caller).
194    pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T> {
195        let bytes = self.backend.get(key).await?;
196        serde_json::from_slice(&bytes).ok()
197    }
198
199    /// Set a key. The value is serialised with serde_json. `ttl =
200    /// None` means no expiry.
201    pub async fn set<T: Serialize + ?Sized>(
202        &self,
203        key: &str,
204        value: &T,
205        ttl: Option<Duration>,
206    ) -> Result<(), serde_json::Error> {
207        let bytes = serde_json::to_vec(value)?;
208        self.backend.set(key, bytes, ttl).await;
209        Ok(())
210    }
211
212    pub async fn delete(&self, key: &str) {
213        self.backend.delete(key).await;
214    }
215
216    pub async fn clear(&self) {
217        self.backend.clear().await;
218    }
219
220    // ── Raw bytes access for cache_page (avoids double-serialisation) ──
221
222    pub(crate) async fn get_bytes_raw(&self, key: &str) -> Option<Vec<u8>> {
223        self.backend.get(key).await
224    }
225
226    pub(crate) async fn set_bytes_raw(&self, key: &str, bytes: Vec<u8>, ttl: Option<Duration>) {
227        self.backend.set(key, bytes, ttl).await;
228    }
229}
230
231// ── MemoryBackend ────────────────────────────────────────────────────────────
232
233struct MemoryEntry {
234    value: Vec<u8>,
235    expires_at: Option<DateTime<Utc>>,
236}
237
238#[derive(Default)]
239pub struct MemoryBackend {
240    inner: Mutex<HashMap<String, MemoryEntry>>,
241}
242
243#[async_trait]
244impl CacheBackend for MemoryBackend {
245    async fn get(&self, key: &str) -> Option<Vec<u8>> {
246        let mut map = self.inner.lock().await;
247        if let Some(entry) = map.get(key) {
248            if let Some(exp) = entry.expires_at {
249                if Utc::now() >= exp {
250                    map.remove(key);
251                    return None;
252                }
253            }
254            return Some(entry.value.clone());
255        }
256        None
257    }
258
259    async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) {
260        let expires_at = ttl.and_then(|d| {
261            chrono::Duration::from_std(d)
262                .ok()
263                .and_then(|cd| Utc::now().checked_add_signed(cd))
264        });
265        self.inner
266            .lock()
267            .await
268            .insert(key.to_string(), MemoryEntry { value, expires_at });
269    }
270
271    async fn delete(&self, key: &str) {
272        self.inner.lock().await.remove(key);
273    }
274
275    async fn clear(&self) {
276        self.inner.lock().await.clear();
277    }
278}
279
280// ── SqliteBackend ────────────────────────────────────────────────────────────
281//
282// CLAUDE.md exception — backend-specific raw SQL is allowed here.
283//
284// The original blockers (no `Vec<u8>` field type, no `upsert`
285// terminal) both shipped in subsequent commits — see
286// `SqlType::Bytes` and `Manager::upsert`. The remaining reason this
287// backend keeps `sqlx::query(...)` calls:
288//
289//   `SqliteBackend` takes an EXPLICIT `SqlitePool` by design (not
290//   the framework's ambient pool). The ORM's `Manager` terminals
291//   read `umbral::db::pool()` for ambient routing; binding them to
292//   a different pool requires an `Manager::upsert_with(&pool, ...)`
293//   escape hatch that doesn't yet exist. Adding it lands when the
294//   first non-ambient-pool consumer asks for it.
295//
296// `Cache::sqlite(pool)` is the explicit-pool entry point — a user
297// who calls it opted into SQLite by name AND into a pool that may
298// be separate from the framework's main pool (cache I/O frequently
299// runs against its own smaller, dedicated pool). The Redis backend
300// below handles the non-SQLite case; an eventual `PgBackend` would
301// be its own sibling.
302
303/// SQLite-backed cache. Table: `umbral_cache(key TEXT PRIMARY KEY,
304/// value BLOB NOT NULL, expires_at TIMESTAMP NULL)`. Expired rows
305/// are skipped on read and removed by [`SqliteBackend::sweep`] for
306/// periodic cleanup.
307pub struct SqliteBackend {
308    pool: SqlitePool,
309}
310
311impl SqliteBackend {
312    pub async fn new(pool: SqlitePool) -> Result<Self, CacheError> {
313        sqlx::query(
314            "CREATE TABLE IF NOT EXISTS umbral_cache (
315                key        TEXT PRIMARY KEY,
316                value      BLOB NOT NULL,
317                expires_at TIMESTAMP NULL
318            )",
319        )
320        .execute(&pool)
321        .await
322        .map_err(CacheError::Sqlx)?;
323        Ok(Self { pool })
324    }
325
326    /// Remove every expired row. Call from a periodic task; reads
327    /// already skip expired rows so a call is never required for
328    /// correctness, only for keeping the table small.
329    pub async fn sweep(&self) -> Result<u64, CacheError> {
330        let result = sqlx::query(
331            "DELETE FROM umbral_cache WHERE expires_at IS NOT NULL AND expires_at <= ?",
332        )
333        .bind(Utc::now())
334        .execute(&self.pool)
335        .await
336        .map_err(CacheError::Sqlx)?;
337        Ok(result.rows_affected())
338    }
339}
340
341#[async_trait]
342impl CacheBackend for SqliteBackend {
343    async fn get(&self, key: &str) -> Option<Vec<u8>> {
344        let row: Option<(Vec<u8>, Option<DateTime<Utc>>)> =
345            sqlx::query_as("SELECT value, expires_at FROM umbral_cache WHERE key = ?")
346                .bind(key)
347                .fetch_optional(&self.pool)
348                .await
349                .ok()?;
350        let (value, expires_at) = row?;
351        if let Some(exp) = expires_at {
352            if Utc::now() >= exp {
353                return None;
354            }
355        }
356        Some(value)
357    }
358
359    async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) {
360        let expires_at = ttl.and_then(|d| {
361            chrono::Duration::from_std(d)
362                .ok()
363                .and_then(|cd| Utc::now().checked_add_signed(cd))
364        });
365        // BROKEN-12: log swallowed write errors. A cache backend is
366        // best-effort (a failed write must not break the request), but a
367        // locked SQLite / dead pool that no-ops every write forever should
368        // not be invisible — the trait doc promised "and log them".
369        if let Err(e) = sqlx::query(
370            "INSERT INTO umbral_cache (key, value, expires_at) VALUES (?, ?, ?)
371             ON CONFLICT(key) DO UPDATE SET value = excluded.value, expires_at = excluded.expires_at",
372        )
373        .bind(key)
374        .bind(value)
375        .bind(expires_at)
376        .execute(&self.pool)
377        .await
378        {
379            tracing::warn!(error = %e, key, "umbral-cache: SQLite cache set failed (swallowed)");
380        }
381    }
382
383    async fn delete(&self, key: &str) {
384        if let Err(e) = sqlx::query("DELETE FROM umbral_cache WHERE key = ?")
385            .bind(key)
386            .execute(&self.pool)
387            .await
388        {
389            tracing::warn!(error = %e, key, "umbral-cache: SQLite cache delete failed (swallowed)");
390        }
391    }
392
393    async fn clear(&self) {
394        if let Err(e) = sqlx::query("DELETE FROM umbral_cache")
395            .execute(&self.pool)
396            .await
397        {
398            tracing::warn!(error = %e, "umbral-cache: SQLite cache clear failed (swallowed)");
399        }
400    }
401}
402
403// ── RedisBackend ─────────────────────────────────────────────────────────────
404
405/// Redis-backed cache. Requires the `redis` cargo feature.
406///
407/// Uses `redis::aio::ConnectionManager` for automatic reconnection. TTL
408/// is stored natively via Redis `SETEX` when a duration is supplied, so
409/// expiry is handled server-side and does not require a background sweep.
410///
411/// `clear()` uses `FLUSHDB` which removes ALL keys in the selected
412/// database — use a dedicated Redis database (e.g. `/1`) when sharing
413/// a Redis instance with other data.
414#[cfg(feature = "redis")]
415pub struct RedisBackend {
416    client: redis::aio::ConnectionManager,
417}
418
419#[cfg(feature = "redis")]
420impl RedisBackend {
421    /// Connect to Redis at `url`. Returns a ready-to-use backend or a
422    /// [`CacheError::Redis`] if the initial connection fails.
423    ///
424    /// `url` form: `redis://[user:pass@]host:port/[db]`
425    /// Example: `redis://localhost:6379/0`
426    pub async fn connect(url: &str) -> Result<Self, CacheError> {
427        let client = redis::Client::open(url).map_err(CacheError::Redis)?;
428        let manager = redis::aio::ConnectionManager::new(client)
429            .await
430            .map_err(CacheError::Redis)?;
431        Ok(Self { client: manager })
432    }
433}
434
435#[cfg(feature = "redis")]
436#[async_trait]
437impl CacheBackend for RedisBackend {
438    async fn get(&self, key: &str) -> Option<Vec<u8>> {
439        use redis::AsyncCommands;
440        let mut conn = self.client.clone();
441        conn.get::<_, Option<Vec<u8>>>(key).await.ok().flatten()
442    }
443
444    async fn set(&self, key: &str, value: Vec<u8>, ttl: Option<Duration>) {
445        use redis::AsyncCommands;
446        let mut conn = self.client.clone();
447        // BROKEN-12: log swallowed errors — a dead Redis that no-ops every
448        // write should not be silent (the trait doc promised "and log them").
449        let res: Result<(), _> = if let Some(dur) = ttl {
450            let secs = dur.as_secs().max(1);
451            conn.set_ex(key, value, secs).await
452        } else {
453            conn.set(key, value).await
454        };
455        if let Err(e) = res {
456            tracing::warn!(error = %e, key, "umbral-cache: Redis cache set failed (swallowed)");
457        }
458    }
459
460    async fn delete(&self, key: &str) {
461        use redis::AsyncCommands;
462        let mut conn = self.client.clone();
463        if let Err(e) = conn.del::<_, ()>(key).await {
464            tracing::warn!(error = %e, key, "umbral-cache: Redis cache delete failed (swallowed)");
465        }
466    }
467
468    async fn clear(&self) {
469        let mut conn = self.client.clone();
470        // FLUSHDB removes all keys in the currently selected database.
471        // Document this prominently: use a dedicated Redis DB for cache.
472        if let Err(e) = redis::cmd("FLUSHDB").query_async::<()>(&mut conn).await {
473            tracing::warn!(error = %e, "umbral-cache: Redis cache clear failed (swallowed)");
474        }
475    }
476}
477
478// ── CacheHeaders config ──────────────────────────────────────────────────────
479
480/// Opt-in HTTP response-header config for `CachePlugin`.
481///
482/// Both knobs are **off by default** — wiring a `CachePlugin` without calling
483/// [`CachePlugin::with_compression`] or [`CachePlugin::cache_control`] leaves
484/// the response pipeline unchanged.
485///
486/// They are independent of and composable with the server-side `cache_page`
487/// store: `cache_page` caches full response bodies, while these knobs emit
488/// HTTP headers that tell downstream clients and proxies how to treat responses.
489///
490/// # Example
491///
492/// ```ignore
493/// App::builder()
494///     .plugin(
495///         CachePlugin::new(Cache::memory())
496///             .with_compression()
497///             .cache_control("public, max-age=3600")
498///             .vary("Accept-Encoding"),
499///     )
500///     .build()
501///     .await?;
502/// ```
503#[derive(Debug, Clone, Default)]
504pub struct CacheHeaders {
505    /// When `true`, applies `tower_http::compression::CompressionLayer` to the
506    /// router. The layer negotiates encoding with the client via
507    /// `Accept-Encoding` and compresses responses with gzip, brotli, deflate,
508    /// or zstd as available.
509    pub compression: bool,
510    /// When `Some(value)`, emits a `Cache-Control` response header on every
511    /// response (using `SetResponseHeaderLayer::overriding`). The value is the
512    /// raw directive string, e.g. `"public, max-age=3600"` or `"no-store"`.
513    pub cache_control: Option<String>,
514    /// When `Some(value)`, emits a `Vary` response header. Common value:
515    /// `"Accept-Encoding"` to tell caches that responses differ by encoding.
516    pub vary: Option<String>,
517}
518
519// ── CachePlugin ──────────────────────────────────────────────────────────────
520
521/// The plugin. Carries no models, no routes — just a `Cache` handle it
522/// installs as the ambient cache at boot, so `cache_page` and any handler
523/// that calls [`ambient()`] find it without explicit dependency injection.
524///
525/// Idiomatic registration (the carried cache is wired in `on_ready`):
526///
527/// ```ignore
528/// App::builder()
529///     .plugin(CachePlugin::new(Cache::memory()))
530///     // or: CachePlugin::new(Cache::redis("redis://localhost:6379/0").await?)
531///     .build()?;
532/// ```
533///
534/// `CachePlugin::init(cache)` remains for manual/test wiring outside the
535/// plugin lifecycle.
536///
537/// ## Opt-in compression and Cache-Control headers
538///
539/// ```ignore
540/// CachePlugin::new(Cache::memory())
541///     .with_compression()               // enables gzip/br/zstd negotiation
542///     .cache_control("public, max-age=3600")
543///     .vary("Accept-Encoding")
544/// ```
545#[derive(Default)]
546pub struct CachePlugin {
547    /// Cache to install as the ambient handle in [`Plugin::on_ready`].
548    /// `None` for the legacy unit-style registration (where the ambient
549    /// cache is wired separately via [`CachePlugin::init`]).
550    cache: Option<Cache>,
551    /// Opt-in HTTP header + compression config. Default: nothing applied.
552    headers: CacheHeaders,
553}
554
555impl CachePlugin {
556    /// Build the plugin carrying `cache`. The idiomatic
557    /// `App::builder().plugin(CachePlugin::new(Cache::memory()))` then
558    /// installs it as the ambient handle at boot (BROKEN-9) — no separate
559    /// `init` call, so `cache_page` actually caches.
560    pub fn new(cache: Cache) -> Self {
561        Self {
562            cache: Some(cache),
563            headers: CacheHeaders::default(),
564        }
565    }
566
567    /// Store `cache` as the ambient handle directly, outside the plugin
568    /// lifecycle. Prefer [`CachePlugin::new`] in app code; this stays for
569    /// manual / test wiring. Calling it twice panics (same contract as
570    /// `settings::init`).
571    pub fn init(cache: Cache) {
572        if AMBIENT_CACHE.set(cache).is_err() {
573            panic!("CachePlugin::init called more than once");
574        }
575    }
576
577    /// Enable response compression. Applies `tower_http::compression::CompressionLayer`
578    /// to the router; negotiates gzip / brotli / deflate / zstd via `Accept-Encoding`.
579    /// Default: off.
580    pub fn with_compression(mut self) -> Self {
581        self.headers.compression = true;
582        self
583    }
584
585    /// Emit a `Cache-Control` header on every response. `value` is the raw
586    /// directive string (e.g. `"public, max-age=3600"`, `"no-store"`).
587    /// Default: not set.
588    pub fn cache_control(mut self, value: impl Into<String>) -> Self {
589        self.headers.cache_control = Some(value.into());
590        self
591    }
592
593    /// Emit a `Vary` header on every response. Typically paired with
594    /// [`with_compression`][Self::with_compression]: `"Accept-Encoding"` tells
595    /// caches that different encodings are distinct variants of the same URL.
596    /// Default: not set.
597    pub fn vary(mut self, value: impl Into<String>) -> Self {
598        self.headers.vary = Some(value.into());
599        self
600    }
601}
602
603impl Plugin for CachePlugin {
604    fn name(&self) -> &'static str {
605        "cache"
606    }
607
608    fn wrap_router(&self, router: Router) -> Router {
609        let h = &self.headers;
610        let mut router = router;
611
612        // Cache-Control header (overriding — the plugin's policy takes
613        // precedence over whatever a handler set).
614        if let Some(ref val) = h.cache_control {
615            if let Ok(hv) = HeaderValue::from_str(val) {
616                router = router.layer(SetResponseHeaderLayer::overriding(CACHE_CONTROL, hv));
617            } else {
618                tracing::warn!(
619                    value = %val,
620                    "CachePlugin: cache_control value contains invalid header characters; \
621                     Cache-Control header will NOT be emitted"
622                );
623            }
624        }
625
626        // Vary header (overriding).
627        if let Some(ref val) = h.vary {
628            if let Ok(hv) = HeaderValue::from_str(val) {
629                router = router.layer(SetResponseHeaderLayer::overriding(VARY, hv));
630            } else {
631                tracing::warn!(
632                    value = %val,
633                    "CachePlugin: vary value contains invalid header characters; \
634                     Vary header will NOT be emitted"
635                );
636            }
637        }
638
639        // Compression (outermost so the body is already compressed before any
640        // header-setter above runs on the response on the way out).
641        if h.compression {
642            router = router.layer(CompressionLayer::new());
643        }
644
645        router
646    }
647
648    fn on_ready(
649        &self,
650        _ctx: &umbral::plugin::AppContext,
651    ) -> Result<(), umbral::plugin::PluginError> {
652        // BROKEN-9: registering the plugin must actually wire the cache,
653        // otherwise `cache_page` silently no-ops on every request. If a
654        // cache was supplied via `new`, install it as the ambient handle.
655        match &self.cache {
656            Some(cache) => {
657                if AMBIENT_CACHE.set(cache.clone()).is_err() {
658                    tracing::warn!(
659                        "CachePlugin::new: an ambient cache was already installed (via \
660                         CachePlugin::init or another CachePlugin); ignoring this one."
661                    );
662                }
663            }
664            None if AMBIENT_CACHE.get().is_none() => {
665                tracing::warn!(
666                    "CachePlugin registered with no cache and none set via CachePlugin::init — \
667                     cache_page layers will silently no-op. Use \
668                     CachePlugin::new(Cache::memory())."
669                );
670            }
671            None => {}
672        }
673        Ok(())
674    }
675}