إعداد Flask-Caching
اختر خلفية التخزين وهيّئ ذاكرة التخزين المؤقت.
إعداد Flask-Caching درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Caching Matters
When the same answer gets computed over and over, you waste time. Caching stores a result once and serves it instantly next time. ⚡
What Flask-Caching Adds
Flask-Caching is an extension that bolts a cache layer onto your app, so you can save view output or function results with almost no code.
Install the Extension
You start by installing the package from PyPI. This pulls in Flask-Caching and makes the Cache class available to import.
pip install Flask-CachingImport the Cache Class
The whole extension centers on one object. You import the Cache class and will create a single instance to manage all cached data.
from flask_caching import CachePick a Cache Backend
A backend decides where cached data lives. SimpleCache keeps it in memory, while Redis or Memcached share it across many processes.
SimpleCache for Local Dev
For learning and tiny apps, SimpleCache is perfect: it stores values in the process memory with zero extra services to run.
config = {"CACHE_TYPE": "SimpleCache"}Configure with a Dict
You tell Flask-Caching how to behave through config keys like CACHE_TYPE. These can sit in your app config or a plain dict.
app.config["CACHE_TYPE"] = "SimpleCache"Create the Cache Object
You build one Cache instance for the whole app. Passing the app now binds the cache immediately so it is ready to use.
cache = Cache(app)The init_app Pattern
With an app factory you defer binding. You create the cache empty, then call init_app later once the app exists.
cache = Cache()
cache.init_app(app)Set a Default Timeout
The CACHE_DEFAULT_TIMEOUT key sets how many seconds entries live before they expire, so stale data does not linger forever.
app.config["CACHE_DEFAULT_TIMEOUT"] = 300Verify It Works
A quick smoke test: store a value and read it straight back. If both lines agree, your cache is wired up correctly. ✅
cache.set("k", 42)
print(cache.get("k"))Quick Check
You want a cache that needs no external server during local development.
Recap: Setup
You installed Flask-Caching, picked a backend, created one Cache object, and set a default timeout. The cache is ready to use. 🎉
الأسئلة الشائعة
هل درس «إعداد Flask-Caching» مجاني؟
نعم — نص درس «إعداد Flask-Caching» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «إعداد Flask-Caching»؟
اختر خلفية التخزين وهيّئ ذاكرة التخزين المؤقت. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «إعداد Flask-Caching»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد Flask-Caching
- تخزين عرض مؤقتًا باستخدام cached
- تخزين الدوال المكلفة مؤقتًا
- إبطال إدخالات ذاكرة التخزين المؤقت وانتهاؤها