التخزين المؤقت لكل عرض ولكل موقع
تخزين الصفحات كاملة مؤقتًا باستخدام decorators
التخزين المؤقت لكل عرض ولكل موقع درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 = 600Only 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/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «التخزين المؤقت لكل عرض ولكل موقع»؟
تخزين الصفحات كاملة مؤقتًا باستخدام decorators تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «التخزين المؤقت لكل عرض ولكل موقع»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- واجهات تخزين Cache وRedis
- التخزين المؤقت لكل عرض ولكل موقع
- التخزين المؤقت لأجزاء القوالب
- واجهة Cache منخفضة المستوى وإبطالها