Michael Ludvig
Jan 11, 2023

The bottom line is that with lru_cache the function is only executed once and on all subsequent calls with the same parameters the results are taken from the cache. If you know what that means for your usecase then it's fine.

But if you expect every call to the function to for example increase some global counter, or make and API call to refresh data, or something else that's non-repeatable, then you shouldn't use lru_cache.

Sometimes it's not this black and white, so just be aware that the function typically only runs only once for every set of parameters, but in fact may actually run again if the cache is full and the cached result was discarded. You can't rely on that either.

Be creative, but be careful :)

Michael Ludvig
Michael Ludvig

Written by Michael Ludvig

GenAI and AWS Cloud ★ Slalom New Zealand

Responses (1)