缓存在数据库中的操作
CACHES={
'default':{
# "BACKEND":'django.core.cache.backends.db.DatabaseCache',
# "LOCATION":'mycache',
# "TIMEOUT":60*5,
"BACKEND":'django_redis.cache.RedisCache',
"LOCATION":'redis://127.0.0.1:6379/1',
"OPTIONS":{
"CLIENT_CLASS":"django_redis.client.DefaultClient",
}
}
}
#@cache_page(30) 利用装饰器实现了缓存,也是存在数据库中
def hello(request):
sleep(5)
return HttpResponse("Hello World!")
def news(request):
result=cache.get("news")
if result:
return HttpResponse(result)
sleep(5)
response= render(request,'company-list.html',context=locals())
cache.set("news",response.content,timeout=60)
return response #这是不加装饰器写的缓存
在这里可以利用装饰器写,也可以利用自己程序来实现缓存
存在自定义的数据库中或者存在redis库中,当然也可以对应多种库进行缓存