Caches and Caching

When you fire up your browser to visit a website, you most probably did not noticed a very important mechanism underlying the browser and working in the background. Caching. Caches are used everywhere to optimize and improve access times and such in consequence the overall performance (of a site). Caching in the browser for example means, that ideally only the portions of the website that changed are loaded from the www. All else was already loaded before and did not change, thus needs no reload. Portions that are no longer up-to-date become invalidated (also known as cache-invalidation) and are loaded anew from the source. There are two types of very different caches in the computer

  1. hardware caches, real physical parts of silicon located near the CPU,

  2. software caches, implemented in the application respectively underlying libraries

The type we want to discuss here are the latter ones.

There are multiple different caching strategies, see also here, but we want to focus on the Last-recently-Used (LRU) caching strategy here. Python’s library functools provides a decorator @lru_cache for this caching strategy. But before just mindlessly using the decorator @lru_cache you should think about the problem itself, as already stated.

Simple example, taken from the python documentation:

from functools import lru_cache

@lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-1) + fib(n-2)

>>> [fib(n) for n in range(16)]
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

>>> fib.cache_info()
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
Last update: May 8, 2025