Django中的缓存
# 缓存框架的核心目标
- 较少的代码
- 缓存应该尽可能快
- 围绕缓存后端的所有代码框架应该保持在绝对最小值,特别是对于获取操作
- 一致性
- 提供跨越不同缓存后端的一致性接口
- 可扩展性
- 基于开发人员需求,缓存 API 应该可以定制化扩展
# 内置缓存
Django 中内置的缓存有以下几种:
- 基于 Memcached 缓存
- 使用数据库进行缓存
- 使用文件系统进行缓存
- 使用本地内存进行缓存
- 提供缓存扩展接口
# 缓存配置
# 一、数据库缓存
# 1、创建缓存表
python manager.py createcachetable my_cache
1
# 2、注册缓存
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache',
'TIMEOUT': '60',
'OPTIONS': {
'MAX_ENTRIES': '300',
},
'KEY_PREFIX': 'joker',
'VERSION': '1',
}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 3、使用
# 3.1、使用装饰器
Django 帮我们封装了一个装饰器 cache_page 来让某一个视图使用缓存,如下:
from django.views.decorators.cache import cache_page
@cache_page(30)
def test_cache(request):
data_list = ['元旦放假了 %s' % x for x in range(10)]
time.sleep(5)
data = {
"status": "ok",
"data": data_list,
}
return render(request, "test_cache.html", locals())
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
第一次请求所花的时间大概是 7s
第二次请求时间大概是 400ms
通过这两次请求可以看到使用缓存的效果是很明显的。
因为我们是使用的数据库作为缓存,我们就可以在数据库中看到其相应的 key,如下:
# 3.2、手动缓存
上面是装饰器自动帮我们缓存数据,我们还可以手动缓存,常用的两个方法是 set()和 get(),如下:
def test_cache(request):
res = cache.get("test_cache")
if res:
return HttpResponse(res)
data_list = ['元旦放假了 %s' % x for x in range(10)]
time.sleep(5)
data = {
"status": "ok",
"data": data_list,
}
response = render(request, "test_cache.html", context=data)
cache.set("test_cache", response, timeout=60)
return response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 二、redis 缓存
Django 除了可以直接使用内置的缓存外还可以使用第三方缓存插件,比如 redis,如果要使用 redis,只需要如下配置:
(1)、安装 django-redis 插件
pip install django-redis
pip install django-redis-cache
1
2
2
(2)、在 settings.py 中配置如下:
CACHES = {
'default': {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
其他的代码都可以不用改,就可以直接使用了。
# 三、混合使用
除了使用上面一种,还可以混合使用,如下:
settings.py 中:
CACHES = {
'default': {
"BACKEND": "django.core.cache.backends.db.DatabaseCache",
"LOCATION": "my_cache",
"TIMEOUT": 60,
},
'redis': {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/1",
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
如果直接使用装饰器的话,就可以直接指定用哪个缓存,如下:
@cache_page(30, cache="default")
def test_cache(request):
data_list = ['元旦放假了 %s' % x for x in range(10)]
time.sleep(5)
data = {
"status": "ok",
"data": data_list,
}
response = render(request, "test_cache.html", context=data)
return response
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
如果是手动缓存,可以如下配置:
def test_cache(request):
cache = caches["default"]
res = cache.get("test_cache")
if res:
return HttpResponse(res)
data_list = ['元旦放假了 %s' % x for x in range(10)]
time.sleep(5)
data = {
"status": "ok",
"data": data_list,
}
response = render(request, "test_cache.html", context=data)
cache.set("test_cache", response, timeout=60)
return response
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 四、总结
在 Django 中可以使用很多种缓存,这里就简单介绍这两种,更多的可以去官方网站或者w3c (opens new window)去查看,下面来总结一些如果在流程种加入缓存,那么在一个时间周期内,流程是怎么走的呢?见下图:
、如果是第一次访问,整个流程就是 1-2-3-4-5-6-7-8-9:
其中 3-7 解释如下:
- 请求进来首先查缓存
- 然后将未找到缓存告诉 views
- views 收到没有缓存则再通过 model 到数据库种查看
- model 将查到的数据返回给 views
- views 将数据加入到缓存中为后续请求使用
那么第二次请求,只要是在缓存有效期内,流程就为 1-2-3-4-8-9
作者:
本文链接:https://jokerbai.com
版权声明:本博客所有文章除特别声明外,均采用 署名-非商业性-相同方式共享 4.0 国际 (CC-BY-NC-SA-4.0) 许可协议。转载请注明出处!
上次更新: 2025/07/19, 11:33:23
- 02
- 使用Zadig从0到1实现持续交付平台07-19
- 03
- 基于Jira的运维发布平台07-19