0Pricing
Django Academy · レッスン

ビュー単位・サイト単位のキャッシュ

デコレーターでページ全体をキャッシュします

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

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

Cache the Whole Page

Sometimes a full page is expensive to build. Django can store the entire rendered response and replay it for the next visitor.

The cache_page Decorator

Wrap a view in cache_page and Django caches its full output for the number of seconds you pass in.

from django.views.decorators.cache import cache_page

@cache_page(60 * 15)
def article_list(request):
    ...

What Per-View Caching Stores

Per-view caching keeps the HttpResponse for that view only, so just the pages you decorate get this speedup.

Caching in the URLconf

You can also apply cache_page in urls.py without touching the view, useful for views you do not own.

path("news/", cache_page(60)(news_view))

Per-Site Caching

To cache nearly every page at once, Django offers per-site caching driven by two middleware classes instead of decorators.

The Two Middleware

Add UpdateCacheMiddleware at the top of the list and FetchFromCacheMiddleware at the bottom for per-site caching to work.

MIDDLEWARE = [
    "django.middleware.cache.UpdateCacheMiddleware",
    "...your middleware...",
    "django.middleware.cache.FetchFromCacheMiddleware",
]

The Site Cache Seconds

Set CACHE_MIDDLEWARE_SECONDS to control how long every cached page stays fresh across the whole site.

CACHE_MIDDLEWARE_SECONDS = 600

Only GET and HEAD

Both strategies cache only safe GET and HEAD requests, so form submissions and other POSTs are never served from cache.

Never Cache Private Pages

Skip caching on user-specific pages with never_cache, since a dashboard must never leak another person's data.

from django.views.decorators.cache import never_cache

@never_cache
def dashboard(request):
    ...

Vary Headers Matter

Use vary_on_headers so a cached page can differ by request details like language or device, keeping the right copy per visitor.

Pick the Right Scope

Reach for per-view caching when only a few heavy pages need help, and per-site when almost everything is read-only.

Quick Check

Let's nail down how Django decides what to cache.

Recap

You learned cache_page for single views and the cache middleware for the whole site, plus never_cache to protect private pages. 🚀

よくある質問

「ビュー単位・サイト単位のキャッシュ」レッスンは無料ですか?

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

「ビュー単位・サイト単位のキャッシュ」で何を学びますか?

デコレーターでページ全体をキャッシュします ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「ビュー単位・サイト単位のキャッシュ」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. Cache BackendとRedis
  2. ビュー単位・サイト単位のキャッシュ
  3. テンプレートフラグメントのキャッシュ
  4. Low-Level cache APIと無効化
← Django Academyに戻る