واجهة Cache منخفضة المستوى وإبطالها
تخزين نتائج الاستعلامات مؤقتًا وحذف المفاتيح القديمة
واجهة Cache منخفضة المستوى وإبطالها درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Cache Anything You Like
The low-level cache API lets you store any Python object, like a query result, with full control over the key and timeout.
Import the cache Object
Reach for the shared cache object from django.core.cache to call its methods anywhere in your views or services.
from django.core.cache import cacheThe Cache-Aside Pattern
The classic flow is cache-aside: try the cache, and only run the slow query when the value is missing.
stats = cache.get("stats")
if stats is None:
stats = compute_stats()
cache.set("stats", stats, 300)get_or_set in One Call
Collapse that pattern with get_or_set, which returns the cached value or computes, stores, and returns it.
stats = cache.get_or_set("stats", compute_stats, 300)Store Many at Once
Use set_many to write several keys in a single round trip, which is faster than many separate set calls.
cache.set_many({"a": 1, "b": 2}, 300)Stale Data Is the Risk
Cached values can drift from the database. Removing an outdated entry so fresh data loads is called invalidation.
Delete a Key
Call delete with a key to drop one entry, forcing the next read to rebuild it from the source.
cache.delete("stats")Invalidate on Save
A reliable habit is to delete the affected key right after the data changes, so users never see the old version.
post.save()
cache.delete("post_" + str(post.id))Versioning Beats Deleting
Pass a version to set and get so bumping the number instantly retires a whole set of old keys at once.
cache.set("stats", data, version=2)Atomic Counters
Increase a numeric key safely with incr, perfect for view counts where several requests update the same value.
cache.incr("page_views")Clear Everything as a Last Resort
The clear method wipes the entire cache. It is a blunt tool, so save it for deploys, not for routine updates.
cache.clear()Quick Check
Let's confirm the cleanest way to read or compute a value.
Recap
You mastered the low-level API: cache-aside with get_or_set, then keep data fresh by deleting keys or bumping the version. 🎯
الأسئلة الشائعة
هل درس «واجهة Cache منخفضة المستوى وإبطالها» مجاني؟
نعم — نص درس «واجهة Cache منخفضة المستوى وإبطالها» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «واجهة Cache منخفضة المستوى وإبطالها»؟
تخزين نتائج الاستعلامات مؤقتًا وحذف المفاتيح القديمة تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «واجهة Cache منخفضة المستوى وإبطالها»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- واجهات تخزين Cache وRedis
- التخزين المؤقت لكل عرض ولكل موقع
- التخزين المؤقت لأجزاء القوالب
- واجهة Cache منخفضة المستوى وإبطالها