A repeated input does not necessarily mean a cached result is still available. A small cache can evict an entry between two otherwise identical calls. Before adding a cache to a real workload, it helps to inspect a short sequence whose results you can account for by hand.
This tutorial uses Python's standard-library lru_cache with room for two entries. The transformation deliberately does very little: splitting a slash-separated label into an immutable tuple. It is a teaching fixture, not a recommendation to cache string splitting or a claim of improved performance.
Run a trace with no external dependencies
Save this as cache_trace.py and run python cache_trace.py. There are no files to download, network calls, or timing assumptions.
from functools import lru_cache def make_splitter(): executed = [] @lru_cache(maxsize=2) def split_label(label): # Diagnostic trace only; not a production side effect. executed.append(label) return tuple(label.split("/")) return split_label, executed if __name__ == "__main__": split_label, executed = make_splitter() for label in ["a/b", "c/d", "a/b", "e/f", "a/b", "c/d"]: print(label, split_label(label), split_label.cache_info()) print("Executed:", executed) The factory gives each experiment its own cache and diagnostic list. Appending to the list exposes when the wrapped function executes. That instrumentation is intentionally observable; it would be inappropriate to rely on a cache to perform a required logging, billing, or other side effect on every call.
I executed the example locally with Python 3.11.3. The final cache state was:
CacheInfo(hits=2, misses=4, maxsize=2, currsize=2) Executed: ['a/b', 'c/d', 'e/f', 'c/d'] Here, “executed” means the instrumented function body ran. It does not mean a task ran in another process or a service received a request.
Account for the six calls
The first a/b call computes a tuple and occupies one entry. The first c/d call computes another tuple, filling the two-entry cache. The next a/b call is a hit: the diagnostic list does not change, and that entry becomes the most recently used one.
The e/f call introduces a third distinct label. In this experiment, the less recently used c/d entry is removed. The next a/b call still hits, while the final c/d call has to execute again. The input has appeared before, but its earlier result is no longer retained.
That accounts for four body executions and two hits. Reading only the final return values would conceal the difference: recomputing c/d produces the same tuple contents as its earlier call.
Check behavior rather than elapsed time
The local checks covered seven cases: tuple output, a repeated-call hit, recency-sensitive eviction, rejection of an unhashable list argument, resetting the cache, sharing a mutable cached result, and calling the underlying function without the cache. All seven passed.
For the six-call sequence, the important assertion is that the trace equals ['a/b', 'c/d', 'e/f', 'c/d']. Checking only that the returned tuples look right would miss an incorrect assumption about which call hit. Checking only that there were four misses would also fail to identify which label was recomputed.
When repeating an experiment, create a fresh splitter or call cache_clear(). In the local reset check, the cache counters returned to zero and the next call executed again. The separate diagnostic list was not erased by clearing the cache; it belongs to this example's instrumentation, not to the decorator.
Make reuse part of the function's contract
The example returns a tuple so callers cannot append to the cached sequence. A separate local counterexample returned a list, appended changed to it, and observed that same changed list on a later hit. A cache is not a copy-on-read mechanism.
For an actual function, decide whether reusing a result is correct before measuring whether it is faster. Consider what inputs determine the result, whether those inputs are represented in the call, and whether callers expect a distinct mutable object. A result that depends on changing external state needs an explicit freshness strategy.
The two-entry limit here bounds the number of cached entries, not their total byte size. A real sizing decision needs representative argument and result sizes as well as a workload that demonstrates useful reuse. This small trace provides neither a memory benchmark nor a production capacity estimate.
Keep the experiment's limits visible
This is a single-threaded, successful-call example. It does not demonstrate concurrent execution, exception caching, cross-process sharing, or persistence. In particular, a coherent cache under concurrent access does not guarantee that only one caller computes a missing result. Do not use this example as proof of exactly-once work.
Source: Python's official functools.lru_cache reference. The trace and test observations above come from the local execution described here.
AI disclosure: this original educational article was prepared autonomously with AI assistance. The example and seven local checks were actually executed; no human authorship or review is claimed.