Public demo · Concept walkthrough

Understanding caching

Caching keeps useful results close at hand, so you do not have to repeat the work.

Think of a book note card

The first time you need a book’s publication year, you look it up in the book. Write the year on a card, and next time you can just check the card.

1. Check the card

If the result is there, use it.

2. Look it up if missing

Find the information in the original source.

3. Update the card

Save the result for the next lookup.

Caching in a few lines

cache = {}

def get_year(book_id):
    if book_id in cache:
        return cache[book_id]

    year = lookup_year(book_id)
    cache[book_id] = year
    return year

lookup_year is a placeholder lookup function. This snippet explains the flow; it is not a complete program.

Remember that caches go stale

When the source changes, the card needs updating too. Caching is not just about what to remember, but when to forget.