Lru_cache stores recent function results under hashable argument keys and evicts older entries when its configured capacity is exceeded.
Python lru_cache: bound retention and include the revision in the key
Operation contract
The price lookup includes both the receipt class and price revision in its key. A repeated revision-one lookup reuses the stored result, while revision two gets its own value. Inspecting cache_info makes retained entry count and reuse visible. Clearing the cache releases its retained key/result references.
Failure and ownership boundary
The cache does not discover that an external price source changed. Omitting revision or another invalidation rule can return stale prices. It also retains returned mutable objects and does not guarantee that a concurrently missed call executes only once. Python hash and equality: immutable dictionary keys and Python SQLite job project: reject conflicting replays by request identity distinguish caching from publication coordination.
Working program
from functools import lru_cache
PRICES = {("standard", 1): 125, ("standard", 2): 150}
@lru_cache(maxsize=2)
def unit_price(receipt_class, revision):
return PRICES[(receipt_class, revision)]
print(unit_price("standard", 1))
print(unit_price("standard", 1))
print(unit_price("standard", 2))
info = unit_price.cache_info()
print("hits:", info.hits, "retained:", info.currsize)
unit_price.cache_clear()
print("after clear:", unit_price.cache_info().currsize)Output
125
125
150
hits: 1 retained: 2
after clear: 0Costs and limits
For k retained entries, the cache owns O(k) key/result references plus their reachable state. A hit avoids the function body but still computes argument-key hashes. Unbounded caches can retain every distinct key; this fixture deliberately uses a capacity.
Common Mistakes
- Cache retention is not a freshness policy.
- A cached mutable result remains shared among callers.
Connected lessons
Python hash and equality: immutable dictionary keys, Python memoization: cache bounded states without hiding recursion depth, Python SQLite job project: reject conflicting replays by request identity.
Check the next state boundary
Python LRU cache project: make misses, replacement and eviction explicit.
Trace the related workflow
Python cached_property: delete the instance value when source state changes.
