使用文件缓存
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
全站缓存
CACHE_MIDDLEWARE_ALIAS = "default"
CACHE_MIDDLEWARE_SECONDS =
300
CACHE_MIDDLEWARE_KEY_PREFIX =
'站点名'
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
用户页面
用户信息页面在登出后不能继续用缓存页面, 而要刷新
使用@vary_on_cookie
装饰器
from django.views.decorators.vary import vary_on_cookie
from django.contrib.auth.decorators import login_required
@login_required(login_url="/login/")
页面级缓存
from django.views.decorators.cache import cache_page,cache_control
@cache_control(must_revalidate=True,max_age=60*5)
基于类的视图缓存
from django.views.decorators.cache import cache_page,cache_control
url(
r'^$',cache_page(60*5)(cache_control(must_revalidate=True,max_age=60*5)(IndexView.as_view()),name="index")),
刷新缓存
from django.core.cache import cache
from django.http import HttpRequest
from django.utils.cache import get_cache_key
def expire_page(request,path):
request2.META[
'SERVER_NAME'] = request.META['SERVER_NAME']
request2.META[
'SERVER_PORT'] = request.META['SERVER_PORT']
request2.path = request.get_host()+
'/'+path
print request2.build_absolute_uri()
key = get_cache_key(request2)
return HttpResponse("OK, the cache is refreshed")
本质上是利用HttpRequest
伪造了一个与想要清除缓存的URL地址相同的请求,通过get_cache_key
这个函数生成cache_key
,然后删除对应的cache,达到刷新页面的效果。函数可以进一步改进至用户前台输入path
,然后清除缓存后通过Ajax
返回成功信息...