Skip to main content

g3_cache/client/
mod.rs

1//! The client cache: see the [crate docs](crate#client-cache) for when to
2//! use it, and [`use_cached`] for how a read is answered.
3
4mod hook;
5mod store;
6
7pub use hook::*;
8
9use crate::{CacheKey, CacheableFn};
10use dioxus::prelude::{GlobalSignal, Signal, document, spawn, use_hook};
11use serde::Serialize;
12use std::{
13    any::Any,
14    borrow::Cow,
15    cell::{Cell, RefCell},
16    collections::BTreeMap,
17    rc::Rc,
18    sync::OnceLock,
19    time::Duration,
20};
21
22/// The server renders for every visitor from one process, so nothing is
23/// cached there: every read fetches, and invalidation does nothing.
24pub(crate) const ENABLED: bool = !cfg!(feature = "server");
25
26/// The `fetched_at` of an entry that must be refetched: invalidated, or
27/// restored from the persistent store.
28pub(crate) const STALE: i64 = i64::MIN;
29
30/// Stored under this key: whose data the persistent store holds.
31const OWNER_KEY: &str = "\u{1f}owner";
32
33/// Settings for the client cache, given once to [`use_client_cache`].
34///
35/// ```
36/// use g3_cache::CacheConfig;
37/// use std::time::Duration;
38///
39/// let config = CacheConfig::new("media-mancer").keep_for(Duration::from_secs(3 * 24 * 60 * 60));
40/// ```
41#[derive(Clone, Debug)]
42pub struct CacheConfig {
43    pub(crate) name: Cow<'static, str>,
44    pub(crate) share_for: Duration,
45    pub(crate) keep_for: Duration,
46    pub(crate) revalidate_on_focus: bool,
47    pub(crate) focus_gap: Duration,
48    pub(crate) max_entries: usize,
49    pub(crate) max_bytes: usize,
50}
51
52impl CacheConfig {
53    /// Settings with the defaults below. `name` names the persistent store
54    /// (the IndexedDB database, or the redb file on mobile), so it must be
55    /// unique to the app on a device or origin.
56    pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
57        Self {
58            name: name.into(),
59            share_for: Duration::from_secs(2),
60            keep_for: Duration::from_secs(7 * 24 * 60 * 60),
61            revalidate_on_focus: true,
62            focus_gap: Duration::from_secs(30),
63            max_entries: 256,
64            max_bytes: 32 * 1024 * 1024,
65        }
66    }
67
68    /// The persistent store's name.
69    pub fn name(&self) -> &str {
70        &self.name
71    }
72
73    /// Maximum answers in each store. Default 256; zero disables storage.
74    pub fn max_entries(mut self, count: usize) -> Self {
75        self.max_entries = count;
76        self
77    }
78
79    /// Maximum combined JSON payload bytes in each store. Default 32 MiB.
80    /// This estimates payload size, not the allocator or database overhead.
81    pub fn max_bytes(mut self, bytes: usize) -> Self {
82        self.max_bytes = bytes;
83        self
84    }
85
86    /// How recent a fetch a screen may reuse instead of repeating it when it
87    /// opens. Default 2 s. This only merges the reads of components that
88    /// mount together (a screen and its children reading the same thing);
89    /// keep it short, since a change made without [`invalidate_cached`] shows only
90    /// once something refetches.
91    pub fn share_for(mut self, duration: Duration) -> Self {
92        self.share_for = duration;
93        self
94    }
95
96    /// How old a stored value may be and still be shown on a cold start
97    /// while its refetch runs. Default 7 days: an older list is more
98    /// misleading than a loading state.
99    pub fn keep_for(mut self, duration: Duration) -> Self {
100        self.keep_for = duration;
101        self
102    }
103
104    /// Whether returning to the app (the tab or window regaining focus, the
105    /// app coming back to the foreground, the network coming back) refetches
106    /// every mounted read. Default on. This is what shows changes made on
107    /// another device on a screen that stayed open; see the
108    /// [crate docs](crate#several-devices).
109    pub fn revalidate_on_focus(mut self, on: bool) -> Self {
110        self.revalidate_on_focus = on;
111        self
112    }
113
114    /// The least time between two refetches caused by focus. Default 30 s,
115    /// so switching windows back and forth doesn't refetch every time.
116    pub fn focus_gap(mut self, duration: Duration) -> Self {
117        self.focus_gap = duration;
118        self
119    }
120}
121
122static CONFIG: OnceLock<CacheConfig> = OnceLock::new();
123
124pub(crate) fn config() -> &'static CacheConfig {
125    CONFIG.get_or_init(|| CacheConfig::new("g3-cache"))
126}
127
128/// Sets up the client cache. Call once, at the top of the app's root
129/// component, before any cached read.
130///
131/// ```ignore
132/// fn App() -> Element {
133///     g3_cache::use_client_cache(g3_cache::CacheConfig::new("media-mancer"));
134///     // ...
135/// }
136/// ```
137///
138/// Without it, reads are still cached with default settings, but a store
139/// named `g3-cache` is shared with any other g3 app on the same origin, and
140/// nothing refetches on focus.
141///
142/// Persisting reads to disk also needs [`set_cache_owner`]: until the app has
143/// said whose data this is, the cache keeps everything in memory only.
144pub fn use_client_cache(config: CacheConfig) {
145    use_hook(move || {
146        // The server renders the root component once per request, and has
147        // no client cache to configure.
148        if !ENABLED {
149            return;
150        }
151        if CONFIG.set(config).is_err() {
152            tracing::warn!(
153                "g3-cache: `use_client_cache` ran more than once, or after the first cached \
154                 read; the later settings are ignored. Call it once, first thing in the root \
155                 component."
156            );
157        }
158        if self::config().revalidate_on_focus {
159            spawn(revalidate_on_focus());
160        }
161    });
162}
163
164/// Refetches every mounted read whenever the app comes back into view.
165/// Page visibility and focus events reach the webview on mobile as well as
166/// the browser, so one listener covers every client.
167async fn revalidate_on_focus() {
168    const SCRIPT: &str = r#"
169        const send = () => dioxus.send(null);
170        document.addEventListener("visibilitychange", () => {
171            if (document.visibilityState === "visible") send();
172        });
173        window.addEventListener("focus", send);
174        window.addEventListener("online", send);
175        await new Promise(() => {});
176    "#;
177    let mut events = document::eval(SCRIPT);
178    let mut last = now_ms();
179    while events.recv::<()>().await.is_ok() {
180        let gap = i64::try_from(config().focus_gap.as_millis()).unwrap_or(i64::MAX);
181        if now_ms().saturating_sub(last) >= gap {
182            last = now_ms();
183            invalidate_all_cached();
184        }
185    }
186}
187
188struct MemoryEntry {
189    value: Rc<dyn Any>,
190    fetched_at: i64,
191    accessed: u64,
192    bytes: usize,
193    measure: fn(&dyn Any) -> usize,
194}
195
196thread_local! {
197    static OWNER_GENERATION: Cell<u64> = const { Cell::new(0) };
198    static ACCESS: Cell<u64> = const { Cell::new(0) };
199    static MEMORY: RefCell<BTreeMap<CacheKey, MemoryEntry>> = const { RefCell::new(BTreeMap::new()) };
200    /// Whether this run has called `set_cache_owner`, which allows writing to the
201    /// persistent store.
202    static OWNER_SET: Cell<bool> = const { Cell::new(false) };
203    /// Whether the persistent store holds a signed-in owner's data, which
204    /// allows reading it. Checked once, on the first restore.
205    static STORE_OWNED: Cell<Option<bool>> = const { Cell::new(None) };
206}
207
208pub(crate) fn owner_generation() -> u64 {
209    OWNER_GENERATION.get()
210}
211
212fn next_access() -> u64 {
213    let next = ACCESS.get().wrapping_add(1);
214    ACCESS.set(next);
215    next
216}
217
218fn trim_memory(memory: &mut BTreeMap<CacheKey, MemoryEntry>, limits: &CacheConfig) {
219    while memory.len() > limits.max_entries
220        || memory.values().map(|entry| entry.bytes).sum::<usize>() > limits.max_bytes
221    {
222        let Some(key) = memory
223            .iter()
224            .min_by_key(|(_, entry)| entry.accessed)
225            .map(|(key, _)| key.clone())
226        else {
227            break;
228        };
229        memory.remove(&key);
230    }
231}
232
233/// Bumped by every invalidation and by [`set_cache_owner`]; every cached read
234/// subscribes to it, so each rechecks its entry (cheaply, unless the entry
235/// was invalidated).
236pub(crate) static EPOCH: GlobalSignal<u64> = Signal::global(|| 0);
237
238pub(crate) fn now_ms() -> i64 {
239    chrono::Utc::now().timestamp_millis()
240}
241
242pub(crate) fn remembered<T: Clone + 'static>(key: &CacheKey) -> Option<(T, i64)> {
243    if !ENABLED {
244        return None;
245    }
246    MEMORY.with_borrow_mut(|memory| {
247        let entry = memory.get_mut(key)?;
248        entry.accessed = next_access();
249        let value = entry.value.downcast_ref::<T>()?.clone();
250        Some((value, entry.fetched_at))
251    })
252}
253
254pub(crate) fn remember<T: Serialize + 'static>(key: &CacheKey, value: T, fetched_at: i64) {
255    if ENABLED {
256        let Ok(bytes) = serde_json::to_vec(&value).map(|json| json.len()) else {
257            return;
258        };
259        if bytes > config().max_bytes || config().max_entries == 0 {
260            MEMORY.with_borrow_mut(|memory| memory.remove(key));
261            return;
262        }
263        let entry = MemoryEntry {
264            value: Rc::new(value),
265            fetched_at,
266            accessed: next_access(),
267            bytes,
268            measure: |value| {
269                value
270                    .downcast_ref::<T>()
271                    .and_then(|value| serde_json::to_vec(value).ok())
272                    .map_or(usize::MAX, |json| json.len())
273            },
274        };
275        MEMORY.with_borrow_mut(|memory| {
276            memory.insert(key.clone(), entry);
277            trim_memory(memory, config());
278        });
279    }
280}
281
282#[derive(serde::Serialize, serde::Deserialize)]
283struct Stored<T> {
284    saved_at: i64,
285    value: T,
286}
287
288pub(crate) async fn restore<T: serde::de::DeserializeOwned>(key: &CacheKey) -> Option<T> {
289    let generation = owner_generation();
290    if !ENABLED || !store_owned().await {
291        return None;
292    }
293    let stored: Stored<T> = serde_json::from_str(&store::load(&key.storage_key()).await?).ok()?;
294    if generation != owner_generation() {
295        return None;
296    }
297    let keep_for = i64::try_from(config().keep_for.as_millis()).unwrap_or(i64::MAX);
298    (now_ms().saturating_sub(stored.saved_at) < keep_for).then_some(stored.value)
299}
300
301pub(crate) async fn persist<T: Serialize>(key: &CacheKey, value: &T) {
302    if !ENABLED || !OWNER_SET.get() {
303        return;
304    }
305    let stored = Stored {
306        saved_at: now_ms(),
307        value,
308    };
309    if let Ok(json) = serde_json::to_string(&stored) {
310        store::save(&key.storage_key(), &json, owner_generation()).await;
311    }
312}
313
314async fn store_owned() -> bool {
315    let generation = owner_generation();
316    if let Some(owned) = STORE_OWNED.get() {
317        return owned;
318    }
319    let owned = store::load(OWNER_KEY)
320        .await
321        .is_some_and(|owner| !owner.is_empty());
322    if generation != owner_generation() {
323        return false;
324    }
325    STORE_OWNED.set(Some(owned));
326    owned
327}
328
329/// Tells the cache whose data it holds: the signed-in user's id, or `None`
330/// when signed out. Call it whenever the signed-in user is known or changes,
331/// including on sign-out.
332///
333/// When the owner differs from the one the persistent store was written
334/// for, or is `None`, the whole cache (memory and store) is emptied, so one
335/// person never sees another's lists on a shared device.
336///
337/// Pass `None` only once you know nobody is signed in, never while still
338/// finding out: it empties the cache, so an app that says "nobody" before
339/// its session check answers starts every launch cold. Resolve the user on
340/// the server (`use_server_future`), or wait for the check.
341///
342/// Until this is first called, the cache **keeps everything in memory
343/// only**. An app that never calls it is still safe, just without instant
344/// cold starts. A store written in an earlier run is still read on a cold
345/// start before this is called: it only ever holds the data of whoever was
346/// signed in last, and this empties it if that is no longer the case.
347pub async fn set_cache_owner(owner: Option<String>) {
348    if !ENABLED {
349        return;
350    }
351    let generation = owner_generation().wrapping_add(1);
352    OWNER_GENERATION.set(generation);
353    OWNER_SET.set(false);
354    STORE_OWNED.set(Some(false));
355    MEMORY.with_borrow_mut(BTreeMap::clear);
356    *EPOCH.write() += 1;
357    let owner = owner.unwrap_or_default();
358    let stored_owner = store::load(OWNER_KEY).await;
359    if generation != owner_generation() {
360        return;
361    }
362    if !owner.is_empty() && stored_owner.as_deref() == Some(owner.as_str()) {
363        STORE_OWNED.set(Some(true));
364        OWNER_SET.set(true);
365        store::prune(generation).await;
366        *EPOCH.write() += 1;
367        return;
368    }
369    store::clear(generation).await;
370    if generation != owner_generation() {
371        return;
372    }
373    store::save(OWNER_KEY, &owner, generation).await;
374    if generation != owner_generation() {
375        return;
376    }
377    STORE_OWNED.set(Some(!owner.is_empty()));
378    OWNER_SET.set(!owner.is_empty());
379    *EPOCH.write() += 1;
380}
381
382fn mark_stale(matches: impl Fn(&CacheKey) -> bool) {
383    if !ENABLED {
384        return;
385    }
386    MEMORY.with_borrow_mut(|memory| {
387        for (key, entry) in memory.iter_mut() {
388            if matches(key) {
389                entry.fetched_at = STALE;
390            }
391        }
392    });
393    *EPOCH.write() += 1;
394}
395
396/// Refetches every cached call of `server_fn`, whatever its arguments.
397///
398/// Call it on the client after a mutation succeeds, once for each read the
399/// mutation changes. Mounted screens refetch and keep showing their old
400/// value until the new one arrives; unmounted ones refetch when they next
401/// open.
402///
403/// ```ignore
404/// save_rating(media_id.clone(), score).await?;
405/// g3_cache::invalidate_cached(get_my_rating);
406/// g3_cache::invalidate_cached(get_my_ratings);
407/// g3_cache::invalidate_cached(get_profile_stats);
408/// ```
409///
410/// Only the mutation knows which reads it affects, so this cannot be
411/// automatic. Forgetting a read leaves it stale until the next screen open
412/// or focus. When unsure, [`invalidate_all_cached`] is always correct, only
413/// slower.
414///
415/// This does not reach the server's caches or Cloudflare. Reads cached
416/// there (see [`cache_shared`](crate::cache_shared)) are refetched through them, and
417/// may come back just as old.
418pub fn invalidate_cached<F, Args>(server_fn: F)
419where
420    F: CacheableFn<Args>,
421{
422    let _ = server_fn;
423    let name = crate::key::fn_name::<F>();
424    mark_stale(|key| key.name() == name);
425}
426
427/// Refetches one cached call: `server_fn` with exactly these `args`.
428///
429/// ```ignore
430/// g3_cache::invalidate_cached_call(get_list_items, (list_id.clone(),));
431/// ```
432pub fn invalidate_cached_call<F, Args>(server_fn: F, args: Args)
433where
434    F: CacheableFn<Args>,
435    Args: Serialize,
436{
437    let target = CacheKey::of(&server_fn, &args);
438    mark_stale(|key| *key == target);
439}
440
441/// Refetches the read cached under exactly `key`.
442pub fn invalidate_cached_key(key: &CacheKey) {
443    mark_stale(|candidate| candidate == key);
444}
445
446/// Refetches every read cached under a [`CacheKey`] named `name`, whatever
447/// its arguments.
448pub fn invalidate_cached_name(name: &str) {
449    mark_stale(|key| key.name() == name);
450}
451
452/// Refetches every cached read. Always correct after a mutation, but every
453/// mounted screen refetches everything it shows; prefer [`invalidate_cached`] for
454/// the reads the mutation changes.
455pub fn invalidate_all_cached() {
456    mark_stale(|_| true);
457}
458
459/// Edits the cached answer of one call, `server_fn` with exactly these
460/// `args`, in place, so a change shows at once rather than after the
461/// mutation's round trip. Returns whether there was an answer to edit.
462///
463/// ```ignore
464/// g3_cache::update_cached(get_playlist, (id.clone(),), |playlist| {
465///     playlist.videos.retain(|video| video.id != removed);
466/// });
467/// remove_from_playlist(id.clone(), removed).await?;
468/// g3_cache::invalidate_cached_call(get_playlist, (id,));
469/// ```
470///
471/// Mounted screens show the edit without refetching. It is memory only: the
472/// persistent store keeps the server's last answer, so **follow the
473/// mutation with an invalidation**, whether it succeeded or not. The
474/// refetch replaces the guess with what the server holds, which also undoes
475/// it if the mutation failed.
476pub fn update_cached<F, Args>(server_fn: F, args: Args, update: impl FnOnce(&mut F::Output)) -> bool
477where
478    F: CacheableFn<Args>,
479    Args: Serialize,
480    F::Output: Clone + 'static,
481{
482    update_cached_key(&CacheKey::of(&server_fn, &args), update)
483}
484
485/// [`update_cached`] for every cached call of `server_fn`, whatever its
486/// arguments: a video marked watched in every cached page of a feed, say.
487/// Returns how many answers were edited.
488pub fn update_all_cached<F, Args>(server_fn: F, mut update: impl FnMut(&mut F::Output)) -> usize
489where
490    F: CacheableFn<Args>,
491    F::Output: Clone + 'static,
492{
493    let _ = server_fn;
494    let name = crate::key::fn_name::<F>();
495    edit_entries(|key| key.name() == name, &mut update)
496}
497
498/// [`update_cached`] for a read cached under a [`CacheKey`] you named.
499pub fn update_cached_key<T: Clone + 'static>(key: &CacheKey, update: impl FnOnce(&mut T)) -> bool {
500    let mut update = Some(update);
501    edit_entries(|candidate| candidate == key, &mut |value: &mut T| {
502        if let Some(update) = update.take() {
503            update(value);
504        }
505    }) > 0
506}
507
508fn edit_entries<T: Clone + 'static>(
509    matches: impl Fn(&CacheKey) -> bool,
510    update: &mut dyn FnMut(&mut T),
511) -> usize {
512    if !ENABLED {
513        return 0;
514    }
515    let edited = MEMORY.with_borrow_mut(|memory| {
516        let mut edited = 0;
517        for (key, entry) in memory.iter_mut() {
518            if !matches(key) {
519                continue;
520            }
521            let Some(current) = entry.value.downcast_ref::<T>() else {
522                continue;
523            };
524            // Copied rather than edited through the `Rc`: a screen still
525            // holding the previous value must see a change arrive, not have
526            // its value altered underneath it.
527            let mut value = current.clone();
528            update(&mut value);
529            entry.bytes = (entry.measure)(&value);
530            entry.accessed = next_access();
531            entry.value = Rc::new(value);
532            edited += 1;
533        }
534        trim_memory(memory, config());
535        edited
536    });
537    if edited > 0 {
538        // Mounted reads recheck their entry. It keeps its `fetched_at`, so a
539        // fresh one shows the edit without refetching and a stale one still
540        // refetches.
541        *EPOCH.write() += 1;
542    }
543    edited
544}
545
546#[cfg(all(test, not(feature = "server")))]
547mod tests {
548    use super::*;
549    use dioxus::prelude::*;
550    use std::sync::atomic::{AtomicUsize, Ordering};
551
552    static FETCHES: AtomicUsize = AtomicUsize::new(0);
553
554    #[test]
555    fn capacity_evicts_the_least_recently_read_payload() {
556        MEMORY.with_borrow_mut(BTreeMap::clear);
557        for name in ["old", "recent", "new"] {
558            remember(&CacheKey::new(name), vec![1_u8; 8], 100);
559        }
560        remembered::<Vec<u8>>(&CacheKey::new("old")).unwrap();
561        MEMORY.with_borrow_mut(|memory| {
562            trim_memory(memory, &CacheConfig::new("test").max_entries(2))
563        });
564        assert!(remembered::<Vec<u8>>(&CacheKey::new("recent")).is_none());
565        assert!(remembered::<Vec<u8>>(&CacheKey::new("old")).is_some());
566        MEMORY
567            .with_borrow_mut(|memory| trim_memory(memory, &CacheConfig::new("test").max_bytes(1)));
568        assert!(MEMORY.with_borrow(BTreeMap::is_empty));
569    }
570
571    #[tokio::test(flavor = "current_thread")]
572    async fn an_owner_switch_hides_a_mounted_answer_before_refetch() {
573        let first_generation = owner_generation();
574        remember(&CacheKey::new("owner-switch"), first_generation, STALE);
575        let mut dom = VirtualDom::new(|| {
576            let cached = use_cached_key(CacheKey::new("owner-switch"), || {
577                let generation = owner_generation();
578                async move { Ok(generation) }
579            });
580            use_context_provider(|| cached);
581            let _ = cached.read();
582            rsx! {}
583        });
584        dom.rebuild_in_place();
585        settle(&mut dom).await;
586        let cached = dom.in_scope(ScopeId::APP, consume_context::<Cached<u64>>);
587        assert_eq!(
588            dom.in_runtime(|| cached.peek().as_ref().unwrap().as_ref().unwrap().to_owned()),
589            first_generation
590        );
591        dom.in_scope(ScopeId::APP, || {
592            spawn(async move {
593                set_cache_owner(Some("another-account".into())).await;
594                // The resource has not rerun yet. Its old signal must be hidden.
595                assert!(cached.peek().is_none());
596            })
597        });
598        settle(&mut dom).await;
599        let answer =
600            dom.in_runtime(|| cached.peek().as_ref().unwrap().as_ref().unwrap().to_owned());
601        assert_eq!(answer, first_generation + 1);
602    }
603
604    fn key() -> CacheKey {
605        CacheKey::new("numbers")
606    }
607
608    #[derive(Props, Clone, PartialEq)]
609    struct Shown {
610        shown: Signal<Option<Vec<u32>>>,
611        pending: Signal<bool>,
612    }
613
614    #[component]
615    fn Reader(props: Shown) -> Element {
616        let Shown {
617            mut shown,
618            mut pending,
619        } = props;
620        let numbers = use_cached_key(key(), || async {
621            FETCHES.fetch_add(1, Ordering::SeqCst);
622            Ok(vec![1, 2, 3])
623        });
624        let value = numbers
625            .read()
626            .as_ref()
627            .and_then(|result| result.as_ref().ok())
628            .cloned();
629        if *shown.peek() != value {
630            shown.set(value);
631        }
632        if *pending.peek() != numbers.pending() {
633            pending.set(numbers.pending());
634        }
635        rsx! {}
636    }
637
638    async fn settle(dom: &mut VirtualDom) {
639        for _ in 0..20 {
640            tokio::select! {
641                () = dom.wait_for_work() => { dom.render_immediate(&mut dioxus_core::NoOpMutations); }
642                () = tokio::time::sleep(std::time::Duration::from_millis(20)) => return,
643            }
644        }
645    }
646
647    /// One test, in order: the steps share the thread-local cache.
648    #[tokio::test(flavor = "current_thread")]
649    async fn an_update_shows_at_once_and_an_invalidation_reconciles_it() {
650        let mut dom = VirtualDom::new(|| {
651            let shown = use_signal(|| None);
652            let pending = use_signal(|| false);
653            use_context_provider(|| (shown, pending));
654            rsx! { Reader { shown, pending } }
655        });
656        dom.rebuild_in_place();
657        settle(&mut dom).await;
658        let (shown, pending) = dom.in_scope(ScopeId::APP, || {
659            consume_context::<(Signal<Option<Vec<u32>>>, Signal<bool>)>()
660        });
661        assert_eq!(dom.in_runtime(|| shown()), Some(vec![1, 2, 3]));
662        assert_eq!(FETCHES.load(Ordering::SeqCst), 1);
663        assert!(!dom.in_runtime(|| pending()));
664
665        let edited =
666            dom.in_runtime(|| update_cached_key(&key(), |numbers: &mut Vec<u32>| numbers.push(4)));
667        assert!(edited);
668        settle(&mut dom).await;
669        assert_eq!(dom.in_runtime(|| shown()), Some(vec![1, 2, 3, 4]));
670        assert_eq!(
671            FETCHES.load(Ordering::SeqCst),
672            1,
673            "an update must not refetch"
674        );
675
676        // A key nothing has cached: nothing to edit, and no wasted rerun.
677        assert!(
678            !dom.in_runtime(|| update_cached_key(&CacheKey::new("absent"), |_: &mut Vec<u32>| {}))
679        );
680
681        dom.in_runtime(|| invalidate_cached_key(&key()));
682        settle(&mut dom).await;
683        assert_eq!(
684            dom.in_runtime(|| shown()),
685            Some(vec![1, 2, 3]),
686            "the refetch replaces the guess"
687        );
688        assert_eq!(FETCHES.load(Ordering::SeqCst), 2);
689        assert!(!dom.in_runtime(|| pending()));
690    }
691}