テンプレートフラグメントのキャッシュ
ページ内の負荷が高い部分だけをキャッシュします
「テンプレートフラグメントのキャッシュ」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Cache Just One Piece
Often only part of a page is slow. Fragment caching lets you cache that one block while the rest renders fresh each time.
Load the cache Tag
Fragment caching lives in a template tag library, so start your template with load cache to make it available.
{% load cache %}The cache Block
Wrap the slow markup in a cache block. Django renders it once, stores the HTML, and reuses it on later requests.
{% cache 500 sidebar %}
...expensive sidebar...
{% endcache %}Timeout Comes First
The first argument is the timeout in seconds. After it expires, the next render rebuilds the fragment and caches it again.
Name Your Fragment
The second argument is a fragment name. Django combines it with extra arguments to form the unique cache key.
Vary the Fragment
Add variables after the name so a fragment can vary per user or object, caching one copy for each distinct value.
{% cache 500 sidebar request.user.id %}
...per-user sidebar...
{% endcache %}Each Variation Is Separate
Every combination of the vary arguments gets its own key, so user 1 and user 2 each see their own cached copy.
Use a Specific Cache
Point a fragment at a named cache with using when you keep page fragments separate from other cached data.
{% cache 500 sidebar using="fragments" %}Great for Shared Widgets
Fragment caching shines for shared widgets like a navbar or a top-posts list that look the same for many visitors.
Keep Live Parts Outside
Leave anything that must stay current, like a greeting with the user's name, outside the cache block so it never goes stale.
Less Effort Than View Caching
Fragment caching is the middle ground: more precise than caching a whole view, yet far simpler than caching every query by hand.
Quick Check
Let's check how a fragment stays unique per user.
Recap
You can now load cache, wrap a fragment in a cache block with a timeout and name, and vary it per user for precise speedups. ✨
よくある質問
「テンプレートフラグメントのキャッシュ」レッスンは無料ですか?
はい。「テンプレートフラグメントのキャッシュ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。
「テンプレートフラグメントのキャッシュ」で何を学びますか?
ページ内の負荷が高い部分だけをキャッシュします ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Django Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「テンプレートフラグメントのキャッシュ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このDjango Academyレッスンでコードを書いて実行できますか?
はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Cache BackendとRedis
- ビュー単位・サイト単位のキャッシュ
- テンプレートフラグメントのキャッシュ
- Low-Level cache APIと無効化