1. Check the card
If the result is there, use it.
Public demo · Concept walkthrough
Caching keeps useful results close at hand, so you do not have to repeat the work.
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.
If the result is there, use it.
Find the information in the original source.
Save the result for the next lookup.
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 yearlookup_year is a placeholder lookup function. This snippet explains the flow; it is not a complete program.
When the source changes, the card needs updating too. Caching is not just about what to remember, but when to forget.