缓存cache
用数据库作缓存
python manage.py createcachetable my_cache_table
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
'TIMEOUT': 60 * 5
}
}
-
缓存使用
-
在视图中使用
-
@cache_page()
- time秒60*5缓存五分钟
- cache 缓存配置,默认为default
- key_prefix前置字符串
-
自己封装
def news(request):
result = cache.get('news')
if result:
return HttpResponse(result)
news_list = ['钓鱼岛是中国的' + str(i) for i in range(10)]
time.sleep(5)
response = render(request, 'news.html', context=locals())
cache.set('news', response.content, timeout=