Use the cache from Effect
Read, write and invalidate cached values from Effect code.
Components aren't the only code that needs the cache: a router loader fetches before a screen shows, a mutation writes what it changed and a sign-out drops everything. This page shows how to read and write the cache from Effect. Use the cache outside Effect covers plain code, and Sign out and switch accounts covers dropping it when the account changes.
The examples import client.ts and todos.ts from Getting started.
Everything the cache does is an Effect. Inside queries and mutations the
client is already there; elsewhere, such as a router loader or an event
handler, run the Effect with queryClient.runPromise, or call the
client's own method for it (Use the cache outside Effect):
import * as from "effect/Effect";
import { } from "@monarcode/effetch-query";
import { } from "./client.js";
import { , } from "./todos.js";
const = (: number) =>
.(function* () {
// The cached value if it's fresh, otherwise a fetch.
const = yield* .(({ : { } }));
// Warm the cache for the next screen. It waits for that fetch, but
// never fails.
yield* .(({ : { : . } }));
return ;
});
export const = (: number) => .(());
export const = () => .(["todos"]);Query.fetch fails as the query does, with its typed error, so
loadTodo rejects when the todo can't load. Query.prefetch never fails:
a failure shows in the query's result instead.
Targets. The functions that act on many queries at once take a target: a query, a family, a key or a group of them, as Targets explains.
The following table lists the cache functions:
| Function | What it does |
|---|---|
Query.fetch(query) | The value if fresh, otherwise a fetch. Joins a fetch in progress. Fails as the query does. |
Query.prefetch(query) | Fetches if not fresh, and ignores the result. Never fails. |
Query.get(query) | The cached value as an Option. Never fetches. |
Query.set(query, value) | Writes a value, which counts as freshly fetched. |
Query.update(query, f) | Writes f(value); does nothing without a value. |
Query.update(family, f) | Writes f(value, args) to every cached query of a family, such as todos. |
Query.invalidate(target) | Marks queries stale: those in use refetch at once, the rest when next used. |
Query.refetch(target) | Refetches the queries in use, and completes when they've settled. |
Query.reset(target) | Drops the values; queries in use fetch again. |
Query.clear(target) | Drops the values without fetching; queries in use show Initial until something asks. |
Query.group(...targets) | One target for several: queries, families, keys or groups. Each matching query once. |
A few details matter when you write to the cache:
- A written value shows until the next fetch settles. It stays while
that fetch is in progress, and if the fetch fails, it stays as the
query's last value. A query that isn't cached yet is cached with it,
without fetching. The client's
querieshooks don't run for a write. Query.updateon a family writes only what's cached. It writes the family's own queries that have a value, and creates none. Writes don't take a group, since one value can't fit queries of different types.Query.getkeeps the last value. A query whose last fetch failed still returns the value it had before.- In use means subscribed. A query is in use while something
subscribes to it, or to an atom or query that depends on it: a component
that shows it, or a query in use that reads it through
ctx.result(Queries that read other queries). A dependent that's only cached doesn't count.
In a mutation's optimistic, Query.set and Query.update writes are
undone if the run fails (Optimistic updates).