Why does a cache make things faster?
A cache keeps reusable results close to where they are needed. A matching valid entry avoids repeating a slower operation.
Identify the request
Build a cache key from the relevant inputs. Different inputs must not accidentally share an entry when their results differ.
Read the complete explanation +
Identify the request
Build a cache key from the relevant inputs. Different inputs must not accidentally share an entry when their results differ.
Check for a usable entry
Look up the key and check validity. An entry may be absent, expired, or invalidated even if it was cached previously.
Reuse a cache hit
When a valid matching entry exists, return it. This saves the time and resources needed to repeat the slower operation.
Fetch on a miss
If there is no usable entry, retrieve or compute the result from the original source. Return it and cache it when permitted.
Keep results appropriate
Expire or invalidate entries as needed. Caching must balance speed, memory use, and the risk of serving stale data.
About this visual model +
A reference desk makes caching tangible: a small set of copies is kept within reach, while an archive holds the originals. An index card identifies the requested item and a date stamp represents validity.
A conceptual cache. Real eviction rules, consistency models, and expiry policies depend on the application.
- Request: Identifies the data or operation being requested.
- Cache key: Selects the exact cached entry. A good key includes every input that changes the result.
- Fast cache: Holds a limited collection of reusable results.
- Original source: Supplies a result when the cache cannot satisfy the request.
- Response: The result returned to the caller.
- Validity check: Determines whether a cached result is still permitted to be reused.
Further reading: HTTP caching ↗