Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Python lru_cache: bound retention and include the revision in the key

Last updated: 30 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

Lru_cache stores recent function results under hashable argument keys and evicts older entries when its configured capacity is exceeded.

Download Python source kit

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

python
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

Output
125
125
150
hits: 1 retained: 2
after clear: 0

Costs 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.

python
functools-cache
Storage details