advanced · 27 minute study
A cache key is a claim that two answers are interchangeable
Caching the wrong scope can serve stale or foreign-user data quickly. Correct caching begins with the meaning and authority of the cached answer.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Choose rendering by freshness, authority and cost — Rendering strategy is a product decision about when HTML is produced and where data becomes available. It determines what hosting can support.
First, build a mental model
A cache reuses a previous result for a key. If two requests share a key, you are claiming the cached result is valid for both under the cache's freshness rules.
- Name the cache: build output, framework data cache, browser memory, HTTP cache and service-worker cache have different lifetimes and controls.
- Include every answer-changing dimension in the cache scope or key. User, company, query and permission changes can affect whether a result is reusable; a correct key still does not replace authorization.
- Define invalidation and staleness explicitly. An older request can arrive after a newer one, so cancellation or response-version checks may be needed even without a persistent cache.
- HTTP Cache-Control influences response reuse: public permits shared-cache use, private restricts shared caching, and no-store forbids storage by compliant caches. Framework caching defaults vary with version/configuration; inspect the actual contract.
type Scope = { actorId: string; companyId: string; query: string };
export function resultKey(scope: Scope): string {
return JSON.stringify([scope.actorId, scope.companyId, scope.query]);
}
// A browser-memory key for synthetic private search results.
// Revalidate authority and clear relevant state on identity changes.- Request A
Alice, company A and query 'lamp' produce a specific key.
- Request B
Bob, company B and the same query must not silently reuse Alice's private result.
- Freshness
Even Alice's key can become stale after a write or permission change; scope and freshness are different questions.
Production thinking
Take this beyond the example.
Test logout, account switching, permission changes, concurrent requests and invalidation. Keep private responses out of shared caches unless the isolation contract has been explicitly designed and verified.
Sources and further reading
Your evidence
unseen is a local learning signal, not a credential or proof of production competence.
Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.
Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.
Next lesson · Read package.json as an executable project contract →