Cache¶
HTTP Guardian cache is deliberately limited to idempotent GET calls. The client does not cache mutations and does not replace an application store.
Enable a Store¶
import { createHttpClient, memoryCache } from "@cedvict/http-guardian";
const api = createHttpClient({
baseUrl,
auth,
parser,
cache: memoryCache({ maxEntries: 500 })
});
Declare Intent per Request¶
api.get<Project[]>("/projects", {
cache: {
ttlMs: 60_000,
tags: ["projects"],
policy: "networkFirst"
}
});
| Policy | Behavior | When to use it |
|---|---|---|
cacheFirst |
Returns valid cached data, otherwise network. | Stable reference data. |
networkFirst |
Tries the network and updates the cache. | Lists that must stay fresh with fallback. |
cacheOnly |
Reads only from cache. | Controlled offline mode. |
networkOnly |
Ignores cache. | Sensitive or highly mutable data. |
staleWhileRevalidate |
Returns cached data, then refreshes in the background. | Smooth UI for frequently loaded lists. |
Invalidation¶
Use stable tags and invalidate them after mutation.
await api.post("/projects", { json: payload, idempotencyKey });
await api.cache.invalidateByTag("projects");
Do not use a signed URL, random query, or temporary identifier as a tag.