given a URL, try finding that page in the cache if the page is in the cache: return the cached page else: generate the page save the generated page in the cache (for next time) return the generated page
Django提供了自己的緩存系統(tǒng),可以讓您保存動態(tài)網(wǎng)頁,為了避免在需要時重新計算它們。Django緩存架構(gòu)的優(yōu)點是,讓你緩存 -
要使用在Django中使用高速緩存,首先要做的是設(shè)置在那里的緩存會保存下來。高速緩存框架提供了不同的可能性 - 高速緩存可以被保存在數(shù)據(jù)庫中,關(guān)于文件系統(tǒng),或直接在內(nèi)存中??稍陧椖康?settings.py 文件設(shè)置完成。
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_table_name',
}
}
對于這項工作,并完成設(shè)置,我們需要創(chuàng)建高速緩存表“my_table_name”。對于這一點,需要做到以下幾點 -
python manage.py createcachetable
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
或
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/tmp/memcached.sock',
}
}
使用高速緩存在Django的最簡單的方法就是緩存整個網(wǎng)站。這可以通過編輯項目settings.py的MIDDLEWARE_CLASSES選項來完成。以下需要添加到選項-
MIDDLEWARE_CLASSES += ( 'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.cache.FetchFromCacheMiddleware', )
CACHE_MIDDLEWARE_ALIAS – The cache alias to use for storage. CACHE_MIDDLEWARE_SECONDS – The number of seconds each page should be cached.
如果不想緩存整個網(wǎng)站,可以緩存特定視圖。這可通過使用附帶 Django 的 cache_page 修飾符完成。我們要緩存視圖viewArticles的結(jié)果-
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def viewArticles(request, year, month): text = "Displaying articles of : %s/%s"%(year, month) return HttpResponse(text)
正如你所看到 cache_page 是您希望視圖結(jié)果被緩存的需要的秒數(shù)(參數(shù))。在上面的例子中,結(jié)果將會緩存 15 分鐘。
urlpatterns = patterns('myapp.views',
url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})/', 'viewArticles', name = 'articles'),)
由于URL使用參數(shù),每一個不同的調(diào)用將被單獨地執(zhí)行緩存。例如,請求 /myapp/articles/02/2007 將分別緩存到 /myapp/articles/03/2008。
緩存一個視圖也可以直接在url.py文件中完成。接著下面有相同的結(jié)果與上所述。只要編輯 myapp/url.py 文件并更改(以上)的相關(guān)映射URL為 -
urlpatterns = patterns('myapp.views',
url(r'^articles/(?P<month>\d{2})/(?P<year>\d{4})/',
cache_page(60 * 15)('viewArticles'), name = 'articles'),)
{% extends "main_template.html" %}
{% block title %}My Hello Page{% endblock %}
{% block content %}
Hello World!!!<p>Today is {{today}}</p>
We are
{% if today.day == 1 %}
the first day of month.
{% elif today == 30 %}
the last day of month.
{% else %}
I don't know.
{%endif%}
<p>
{% for day in days_of_week %}
{{day}}
</p>
{% endfor %}
{% endblock %}
{% load cache %}
{% extends "main_template.html" %}
{% block title %}My Hello Page{% endblock %}
{% cache 500 content %}
{% block content %}
Hello World!!!<p>Today is {{today}}</p>
We are
{% if today.day == 1 %}
the first day of month.
{% elif today == 30 %}
the last day of month.
{% else %}
I don't know.
{%endif%}
<p>
{% for day in days_of_week %}
{{day}}
</p>
{% endfor %}
{% endblock %}
{% endcache %}
正如你可以在上面看到,緩存標簽將需要2個參數(shù) ? 想要的塊被緩存(秒)以及名稱提供給緩存片段。