BeginnerCoder

Lesson 22 — Cache decorator (expert)

Remember slow answers

A decorator is a function that wraps another function to add behaviour; you apply it with @name above the def. A cache decorator stores results by argument, so the second call with the same input skips the slow work. Django has @cache_page for whole views.

if city not in weather:
    weather[city] = fetch(city)   # only the first time
return weather[city]

Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.

Why it matters: caching is the cheapest way to make a busy page fast.

Want it another way?
Task

Finish inner() so results are stored in store. The slow function should run only once for 5.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Django Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.