一句话总结:缓存能够对view、模板、数据进行缓存能够设置缓存在不一样的地方(本地内存、redis、系统文档)能够为服务器节省性能、减小用户等待时间。redis
CACHES={ 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', #缓存到本地内存中 'TIMEOUT': 60, } }
安装包:pip install django-redis-cache CACHES "default": { "BACKEND": "redis_cache.cache.RedisCache", #缓存到redis中 "LOCATION": "localhost:6379", 'TIMEOUT': 60, }, }
链接:redis-cli 切换数据库:select 1 查看键:keys * 查看值:get 键
from django.views.decorators.cache import cache_page @cache_page(60 * 15) def index(request): return HttpResponse('hello1') #return HttpResponse('hello2')
{% load cache %} {% cache 500 hello %} hello1 <!--hello2--> {% endcache %}
from django.core.cache import cache 设置:cache.set(键,值,有效时间) 获取:cache.get(键) 删除:cache.delete(键) 清空:cache.clear()