cache¶
Cache results by arguments, and serve the last good value when a refresh fails.
from nopanic import cache
@cache(ttl=60.0, stale_ttl=3600.0, on=(ConnectionError, TimeoutError))
def exchange_rates(base): ...
- Values younger than
ttlare served without calling. - When a refresh fails with an exception matching
on, an expired value is served for up tostale_ttlmore seconds instead of the error (eventcache.stale_served).stale_ttl=0makes it a plain TTL cache. - Single-flight: concurrent misses of one key make one call; every caller gets its outcome (value, stale value or the same exception). A cancelled leader hands over to its followers.
single_flight=Falseturns it off. - LRU-bounded by
maxsize(default 1024);cache.size,cache.clear(). - Keys are the arguments, which must be hashable; pass
key=lambda *args, **kwargs: ...otherwise. One instance may decorate several functions; entries are kept per function. - Streams cannot be cached (
TypeErrorat decoration time).
Events¶
cache.stale_served (age, exception).