واجهات تخزين Cache وRedis
تهيئة واجهة تخزين Cache لبيئة الإنتاج
واجهات تخزين Cache وRedis درس مجاني في Django Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Django Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Django Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
What a Cache Saves You
A cache stores expensive results so the next request can skip the slow work and reply almost instantly. ⚡
Where Caches Live
Django talks to a cache through a backend, a pluggable adapter that decides where your cached data is physically stored.
The CACHES Setting
You configure caching in the CACHES dict in settings.py, always with a key named default that Django reads first.
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
}
}Local Memory for Dev
The default LocMemCache keeps data in your process memory. It is fine for development but is not shared across servers.
Why Redis in Production
For real apps, point the backend at Redis, a fast in-memory store that many web servers can share at once.
Configuring the Redis Backend
Set BACKEND to RedisCache and give it a LOCATION pointing at your Redis server URL.
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}Talking to the Cache
Import the shared cache object from django.core.cache to read and write values from anywhere in your code.
from django.core.cache import cacheset and get
Use set to store a value under a key, then get to read it back later in another request.
cache.set("answer", 42)
value = cache.get("answer")Timeouts Keep Data Fresh
The third argument to set is a timeout in seconds. After it passes, the key expires and get returns None.
cache.set("answer", 42, 300)Handling Cache Misses
A missing key is a cache miss. Pass a default to get so your code has a safe value to fall back on.
value = cache.get("answer", "none yet")Other Backends Exist
Beyond Redis, Django ships database and file-based backends, handy when you cannot run a separate memory server.
Quick Check
Time to confirm where production caches usually live.
Recap
You met the cache backend: configure CACHES, use LocMem in dev and Redis in production, then set and get values with a timeout. 🎉
الأسئلة الشائعة
هل درس «واجهات تخزين Cache وRedis» مجاني؟
نعم — نص درس «واجهات تخزين Cache وRedis» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Django Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Django Academy 4 دروس في المجموع.
ماذا ستتعلم في «واجهات تخزين Cache وRedis»؟
تهيئة واجهة تخزين Cache لبيئة الإنتاج تتمرن على Django Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Django Academy؟
لا تُشترط خبرة سابقة. Django Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «واجهات تخزين Cache وRedis»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Django Academy هذا؟
نعم. كل درس في Django Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- واجهات تخزين Cache وRedis
- التخزين المؤقت لكل عرض ولكل موقع
- التخزين المؤقت لأجزاء القوالب
- واجهة Cache منخفضة المستوى وإبطالها