تخزين الدوال المكلفة مؤقتًا
خزّن نتائج الدوال مؤقتًا حسب الوسائط.
تخزين الدوال المكلفة مؤقتًا درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Beyond Whole Views
Sometimes only one slow helper is the problem, not the whole page. Memoization caches a function result keyed by its arguments.
The memoize Decorator
You wrap a function with @cache.memoize(). Same arguments return the stored result instead of recomputing it. 💡
@cache.memoize()
def heavy(n):
return slow_calc(n)Keyed by Arguments
Memoize builds the key from the arguments. heavy(2) and heavy(5) are cached separately, each remembering its own answer.
cached vs memoize
Use cached for views keyed by URL, and memoize for plain functions keyed by their inputs. They solve different layers.
Set a memoize Timeout
Just like views, you give memoize a timeout so results expire. After it passes, the next call recomputes and re-stores.
@cache.memoize(timeout=120)
def rates():
...Perfect for Pure Functions
Memoize shines on pure functions: same input always gives the same output, with no side effects to lose.
Avoid Caching Side Effects
Do not memoize a function that sends email or writes a row. On a cache hit the body is skipped, so the effect never runs.
Methods Need Care
For instance methods, the key includes self. Two objects cache separately, which is usually exactly what you want.
Drop One Memoized Result
You can evict a single entry with delete_memoized, passing the function and the exact arguments to clear.
cache.delete_memoized(rates)Clear by Argument
Pass arguments to delete_memoized to drop just that variant, leaving every other cached result untouched.
cache.delete_memoized(heavy, 5)Measure Before Memoizing
Only memoize what is actually slow. Profile first, because caching cheap work adds key overhead with little real gain.
Quick Check
You memoize a function that also logs each call to your database.
Recap: Memoization
You can memoize slow pure functions, key results by arguments, set timeouts, and evict with delete_memoized. 🎯
الأسئلة الشائعة
هل درس «تخزين الدوال المكلفة مؤقتًا» مجاني؟
نعم — نص درس «تخزين الدوال المكلفة مؤقتًا» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «تخزين الدوال المكلفة مؤقتًا»؟
خزّن نتائج الدوال مؤقتًا حسب الوسائط. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تخزين الدوال المكلفة مؤقتًا»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد Flask-Caching
- تخزين عرض مؤقتًا باستخدام cached
- تخزين الدوال المكلفة مؤقتًا
- إبطال إدخالات ذاكرة التخزين المؤقت وانتهاؤها