实现缓存的方式,有多种,本地内存缓存,数据库缓存,文件系统缓存。这里介绍使用Redis数据库进行缓存。

缓存有站点缓存,和单个view缓存
MIDDLEWARE = [ # 站点缓存 , 注意必须在第一个位置
... # 站点缓存, 注意必须在最后一个位置
from django.shortcuts import renderfrom django.views.decorators.cache import cache_pagefrom cache.models import Foo# 在需要缓存的视图上添加装饰器, 参数是设置timeout 超时时间, 单位是秒, @cache_page(60)def index(request):