Cache Results with LRU Cache

Talk it through out loud with your AI coach.

The problem

  • medium
  • Pro

A function that converts currency codes to display names is called thousands of times with the same few codes, and a colleague suggests decorating it with lru cache from functools. Explain what that does, its limits, and when caching like this goes wrong.

What a strong answer covers

  • What memoisation is and how the decorator decides a call is a repeat.
  • What kinds of arguments it can and can't cache.
  • Controlling the cache's size and inspecting or resetting it.
  • Situations where a cache returns stale or shared results, or holds memory too long.

Common mistake: Adding the cache to a function that reads changing data, such as the database, so callers quietly get old answers.

Knowing this is half of it. Saying it clearly out loud is the part the coach helps you practise.

Practise it out loud