LLearnCode
Academy/web-systems/Data and runtime design

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.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

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.

  1. Name the cache: build output, framework data cache, browser memory, HTTP cache and service-worker cache have different lifetimes and controls.
  2. 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.
  3. 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.
  4. 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.
Read the codetypescript · educational example
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.
  1. Request A

    Alice, company A and query 'lamp' produce a specific key.

  2. Request B

    Bob, company B and the same query must not silently reuse Alice's private result.

  3. 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 →