0Pricing
Django Academy · บทเรียน

การแคชรายวิวและรายเว็บไซต์

แคชทั้งหน้าโดยใช้ดีคอเรเตอร์

การแคชรายวิวและรายเว็บไซต์ เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 🚀

คำถามที่พบบ่อย

บทเรียน “การแคชรายวิวและรายเว็บไซต์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแคชรายวิวและรายเว็บไซต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแคชรายวิวและรายเว็บไซต์”

แคชทั้งหน้าโดยใช้ดีคอเรเตอร์ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การแคชรายวิวและรายเว็บไซต์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แบ็กเอนด์แคชและ Redis
  2. การแคชรายวิวและรายเว็บไซต์
  3. การแคชบางส่วนของเทมเพลต
  4. API แคชระดับต่ำและการทำให้แคชเป็นโมฆะ
← กลับไปที่ Django Academy