English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
لحفظ نتائج الحسابات المكلفة، لا تحتاج إلى تنفيذها مرة أخرى عند الحاجة إليها. إليك شيفرة وهمية توضح كيف يعمل الحماية -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 بإعطاء عنوان URL، حاول العثور على تلك الصفحة في الحالة إذا كانت الصفحة موجودة في الحالة: العودة إلى الصفحة المحفوظة else: توليد الصفحة حفظ الصفحة المولدة في الحالة (للمرة القادمة) العودة إلى الصفحة المولدة
يقدم Django نظام حماية خاص به، مما يتيح لك حفظ صفحات الويب الديناميكية لتجنب إعادة حسابها عند الحاجة. من بين مزايا هيكل حماية Django، السماح بحماية
الناتج من الرؤية المحددة جزء من القالب الموقع الكامل
لإستخدام الحالة السريعة في Django، أولاً، يجب عليك إعداد الحالة التي سيتم حفظها هناك. يقدم إطار الحالة إمكانيات مختلفة - يمكن حفظ الحالة في قاعدة البيانات، في نظام الملفات، أو مباشرة في الذاكرة. يمكن إعدادها في ملف settings.py للمشروع.
Just add the following to the project's settings.py file-
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', 'LOCATION': 'my_table_name', } }
For this task, and to complete the setup, we need to create a high-speed cache table 'my_table_name'. For this, the following points need to be done -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 python manage.py createcachetable
Set up caching in the file system
Just add the following to the project's settings.py file-
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': '/var/tmp/django_cache', } }
Set the cache in memory
This is the most effective method of caching, you can use it depending on the Python binding library that has chosen the memory high-speed cache, as shown in the following options -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', } }
Or
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': 'unix:/tmp/memcached.sock', } }
Cache the entire website
The simplest way to use high-speed caching in Django is to cache the entire website. This can be done by editing the MIDDLEWARE_CLASSES option in the project's settings.py file. The following needs to be added to the option-
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 MIDDLEWARE_CLASSES += ( 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', )
Please note that the order here is very important, the update should be done before obtaining the middleware.
Then, in the same file, it is also necessary to set -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 CACHE_MIDDLEWARE_ALIas – The cache alias to use for storage. CACHE_MIDDLEWARE_SECONDS – عدد الثواني التي يجب أن يتم تثبيت كل صفحة.
تثبيت عرض
إذا كنت لا تريد تثبيت الموقع بأكمله، يمكنك تثبيت عرض معين. هذا يمكن القيام به باستخدام مدير التثبيت المدمج في Django، cache_page. نريد تثبيت نتائج عرض viewArticles
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 from django.views.decorators.cache import cache_page @cache_page(60 * 15) def viewArticles(request, year, month): text = "عرض المقالات الخاصة ب: %s/%s"%(year, month) return HttpResponse(text)
كما رأيت، cache_page هو عدد الثواني الذي ترغب في تثبيت نتائج عرضك (المعلمة). في هذا المثال، سيتم تثبيت النتيجة لمدة 15 دقيقة.
ملاحظة - كما رأينا من قبل، يتم ربط هذه النظرة ب -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 urlpatterns = patterns('myapp.views', url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})/', 'viewArticles', name = 'articles'),)
بسبب استخدام URL للمعلمات، يتم تنفيذ كل طلب مختلف بشكل منفرد. على سبيل المثال، الطلب /myapp/articles/02/2007 سيتم تثبيته في /myapp/articles/03/2008.
يمكن تثبيت نموذج عرض أيضًا مباشرة في ملف url.py. بعد ذلك، هناك نتائج مشابهة لتلك المذكورة أعلاه. فقط قم بتعديل ملف myapp/url.py وغيير الارتباطات URL المذكورة أعلاه إلى -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 urlpatterns = patterns('myapp.views', url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})/', cache_page(60 * 15)('viewArticles'), name = 'articles'),)
بالطبع، لم يعد يحتاج إلى myapp/views.py.
تثبيت أجزاء النمط
يمكن أيضًا تثبيت جزء من النمط، ويتم ذلك باستخدام العلامة cache. دعونا نعدل نمط hello.html وتابع -
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 {% extends "main_template.html" %} {% block title %}صفحة مرحبًا بي{% endblock %} {% block content %} مرحبًا بالعالم!!!<p اليوم هو {{today}}</p> نحن {% if today.day == 1 %} أول يوم في الشهر. {% elif today == 30 %} آخر يوم في الشهر. {% else %} لا أعرف. {% endif %} <p> {% for day in days_of_week %} {{day}} </p> {% endfor %} {% endblock %}
سيكون جزء المحتوى المخزن -}}
# ملف: example.py # حقوق الطبع والنشر: 2020 بواسطة w3codebox # كاتب: ar.oldtoolbag.com # تاريخ: 2020-08-08 {% load cache %} {% extends "main_template.html" %} {% block title %}صفحة مرحبًا بي{% endblock %} {% cache 500 content %} {% block content %} مرحبًا بالعالم!!!<p اليوم هو {{today}}</p> نحن {% if today.day == 1 %} أول يوم في الشهر. {% elif today == 30 %} آخر يوم في الشهر. {% else %} لا أعرف. {% endif %} <p> {% for day in days_of_week %} {{day}} </p> {% endfor %} {% endblock %} {% endcache %}
كما يمكنك رؤيته أعلاه، سيحتاج علامة التبويب المخزنة إلى 2 معامل - الوقت الذي سيتم فيه حفظ الكتلة المطلوبة (بالدقائق) والاسم المقدم للجزء المخزن.