使用中間件,通過一系列的認證等操做,若是內容在緩存中存在,則使用FetchFromCacheMiddleware獲取內容並返回給用戶,當返回給用戶以前,判斷緩存中是否已經存在,若是不存在則UpdateCacheMiddleware會將緩存保存至緩存,從而實現全站緩存 MIDDLEWARE = [ 'django.middleware.cache.UpdateCacheMiddleware', # 其餘中間件... 'django.middleware.cache.FetchFromCacheMiddleware', ] CACHE_MIDDLEWARE_ALIAS = "" CACHE_MIDDLEWARE_SECONDS = "" CACHE_MIDDLEWARE_KEY_PREFIX = ""
方式一: from django.views.decorators.cache import cache_page @cache_page(60 * 15) def my_view(request): ... 方式二: from django.views.decorators.cache import cache_page urlpatterns = [ url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)), ]
a. 引入TemplateTag {% load cache %} b. 使用緩存 {% cache 5000 緩存key %} 緩存內容 {% endcache %}
更多:猛擊這裏html
測試:python
若是緩存生效了,那麼刷新頁面,時間不會發生變化,由於是從緩存中取得redis
from django.shortcuts import render, HttpResponse import time from django.views.decorators.cache import cache_page from django_redis import get_redis_connection conn = get_redis_connection("default") # @cache_page(60*15) # s 15min def index(request): ctime = str(time.time()) return HttpResponse(ctime) def order(request): ctime = str(time.time()) return HttpResponse(ctime)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# 此爲開始調試用,實際內部不作任何操做
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.dummy.DummyCache'
,
# 引擎
'TIMEOUT'
:
300
,
# 緩存超時時間(默認300,None表示永不過時,0表示當即過時)
'OPTIONS'
:{
'MAX_ENTRIES'
:
300
,
# 最大緩存個數(默認300)
'CULL_FREQUENCY'
:
3
,
# 緩存到達最大個數以後,剔除緩存個數的比例,即:1/CULL_FREQUENCY(默認3)
},
'KEY_PREFIX'
: '',
# 緩存key的前綴(默認空)
'VERSION'
:
1
,
# 緩存key的版本(默認1)
'KEY_FUNCTION'
函數名
# 生成key的函數(默認函數會生成爲:【前綴:版本:key】)
}
}
# 自定義key
def
default_key_func(key, key_prefix, version):
"""
Default function to generate keys.
Constructs the key used by all other methods. By default it prepends
the `key_prefix'. KEY_FUNCTION can be used to specify an alternate
function with custom key making behavior.
"""
return
'%s:%s:%s'
%
(key_prefix, version, key)
def
get_key_func(key_func):
"""
Function to decide which key function to use.
Defaults to ``default_key_func``.
"""
if
key_func
is
not
None
:
if
callable
(key_func):
return
key_func
else
:
return
import_string(key_func)
return
default_key_func
|
1
2
3
4
5
6
7
8
9
10
|
# 此緩存將內容保存至內存的變量中
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.locmem.LocMemCache'
,
'LOCATION'
:
'unique-snowflake'
,
#給緩存放置的內存區設置一個名字
}
}
# 注:其餘配置同開發調試版本
|
1
2
3
4
5
6
7
8
9
10
|
# 此緩存將內容保存至文件
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.filebased.FileBasedCache'
,
'LOCATION'
:
'/var/tmp/django_cache'
,
#配置緩存存放的目錄
}
}
# 注:其餘配置同開發調試版本
|
1
2
3
4
5
6
7
8
9
10
11
|
# 此緩存將內容保存至數據庫
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.db.DatabaseCache'
,
'LOCATION'
:
'my_cache_table'
,
# 數據庫表
}
}
# 注:須要執行建立表命令 python manage.py createcachetable,這樣會額外建立一張表來存放緩存數據
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 此緩存使用python-memcached模塊鏈接memcache
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'
,
}
}
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.memcached.MemcachedCache'
,
'LOCATION'
: [
'172.19.26.240:11211'
,
'172.19.26.242:11211'
,
]
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# 此緩存使用pylibmc模塊鏈接memcache
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.memcached.PyLibMCCache'
,
'LOCATION'
:
'127.0.0.1:11211'
,
}
}
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.memcached.PyLibMCCache'
,
'LOCATION'
:
'/tmp/memcached.sock'
,
}
}
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.memcached.PyLibMCCache'
,
'LOCATION'
: [
'172.19.26.240:11211'
,
'172.19.26.242:11211'
,
]
}
}
|
上述都是一些基本的配置,更重要的是配置以後去應用它。數據庫
1
2
3
4
5
6
7
8
9
10
11
|
使用中間件,通過一系列的認證等操做,若是內容在緩存中存在,則使用FetchFromCacheMiddleware獲取內容並返回給用戶,當返回給用戶以前,判斷緩存中是否已經存在,若是不存在則UpdateCacheMiddleware會將緩存保存至緩存,從而實現全站緩存
MIDDLEWARE
=
[
'django.middleware.cache.UpdateCacheMiddleware'
,
#放到第一個中間件位置
# 其餘中間件...
'django.middleware.cache.FetchFromCacheMiddleware'
,
#放到最後一個
]
CACHE_MIDDLEWARE_ALIAS
=
""
CACHE_MIDDLEWARE_SECONDS
=
""
CACHE_MIDDLEWARE_KEY_PREFIX
=
""
|
1
2
3
4
5
6
7
8
9
10
11
12
|
方式一:裝飾器
from
django.views.decorators.cache
import
cache_page
@cache_page
(
60
*
15
)
def
my_view(request):
...
<br>方式二:裝飾器的另一種寫法
from
django.views.decorators.cache
import
cache_page
urlpatterns
=
[
url(r
'^foo/([0-9]{1,2})/$'
, cache_page(
60
*
15
)(my_view)),
]
|
1
2
3
4
5
6
7
8
9
|
a. 引入TemplateTag
{
%
load cache
%
}
b. 使用緩存
{
%
cache
5000
緩存key
%
}
#這裏是緩存5秒
緩存內容
{
%
endcache
%
}
|