0Pricing
Flask Academy · レッスン

cachedでビューをキャッシュする

URLごとにレスポンス全体をメモ化します

「cachedでビューをキャッシュする」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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. 🚀

よくある質問

「cachedでビューをキャッシュする」レッスンは無料ですか?

はい。「cachedでビューをキャッシュする」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「cachedでビューをキャッシュする」で何を学びますか?

URLごとにレスポンス全体をメモ化します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「cachedでビューをキャッシュする」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Flask-Cachingをセットアップする
  2. cachedでビューをキャッシュする
  3. コストの高い関数をメモ化する
  4. キャッシュエントリを無効化して期限切れにする
← Flask Academyに戻る