Cache a View with cached
Memoize whole responses by URL.
Cache a View with cached is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Caching Whole Responses
Some pages cost real work to build. The cached decorator stores the entire response, so repeat visitors get an instant copy.
Apply the Decorator
You add @cache.cached() right above your view function. The first request runs it; later ones replay the stored result.
@app.route("/report")
@cache.cached()
def report():
return build_report()Order of Decorators
Stacking matters. Put @app.route on top and @cache.cached below it, so routing happens before caching kicks in.
Set a Per-View Timeout
You can override the global setting per view. The timeout argument says how many seconds this specific response stays fresh.
@cache.cached(timeout=60)
def report():
...Cached by Request Path
By default the cache key is the request path. So /report and /report/extra are stored as separate, independent entries.
Query Strings Are Ignored
Watch out: the default key skips the query string. So /list?page=2 and /list?page=3 would share one cached entry.
Vary on the Full URL
To split by query string, set query_string=True. Now each unique URL, parameters included, gets its own cached response.
@cache.cached(query_string=True)
def listing():
...Custom Cache Keys
Need full control? Pass a make_cache_key function that builds the key yourself, for example mixing in the current user.
@cache.cached(make_cache_key=my_key)Only Cache Safe Methods
Cache GET requests, never POST. Caching a write would replay an old body and skip the side effects users expect.
Skip the Cache When Needed
Return unless a condition holds and the view runs fresh every time. Handy to bypass caching for logged-in users.
@cache.cached(unless=lambda: g.user)First Hit vs Later Hits
The first request is a cache miss and does the work. Every later request within the timeout is a fast cache hit.
Quick Check
Your list page uses ?page= but every page shows the same cached content.
Recap: View Caching
You learned to wrap views with @cache.cached, tune the timeout, key on the full URL, and skip caching for writes. 🚀
Frequently asked questions
Is the “Cache a View with cached” lesson free?
Yes — the full text of “Cache a View with cached” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cache a View with cached”?
Memoize whole responses by URL. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache a View with cached” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Set Up Flask-Caching
- Cache a View with cached
- Memoize Expensive Functions
- Invalidate and Expire Cache Entries