- 开发调试
- 内存
- 文件
- 数据库
- Memcache缓存(python-memcached模块)
- Memcache缓存(pylibmc模块)
1. 开发调试
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
|
2. 内存
1
2
3
4
5
6
7
8
9
10
|
# 此缓存将内容保存至内存的变量中
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.locmem.LocMemCache'
,
'LOCATION'
:
'unique-snowflake'
,
#给缓存放置的内存区设置一个名字
}
}
# 注:其他配置同开发调试版本
|
3. 文件
1
2
3
4
5
6
7
8
9
10
|
# 此缓存将内容保存至文件
# 配置:
CACHES
=
{
'default'
: {
'BACKEND'
:
'django.core.cache.backends.filebased.FileBasedCache'
,
'LOCATION'
:
'/var/tmp/django_cache'
,
#配置缓存存放的目录
}
}
# 注:其他配置同开发调试版本
|
4. 数据库
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,这样会额外创建一张表来存放缓存数据
|
5. Memcache缓存(python-memcached模块)
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'
,
]
}
}
|
6. Memcache缓存(pylibmc模块)
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. 全站使用缓存
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
=
""
|
2. 单独视图缓存
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)),
]
|
3. 局部视图缓存
1
2
3
4
5
6
7
8
9
|
a. 引入TemplateTag
{
%
load cache
%
}
b. 使用缓存
{
%
cache
5000
缓存key
%
}
#这里是缓存5秒
缓存内容
{
%
endcache
%
}
|