div>此处的上游层次可能定义不是太准确,指ISP,代理,或者web浏览器提供的缓存

在此类缓存情况下,django1.3之后通过url和query(查询条件)来使用缓存,这种情况对于安全性有隐患
可能需要使用变量(vary)头部来处理
使用方式如下:

from django.views.decorators.vary import vary_on_headers

@vary_on_headers('User-Agent') #使用user-agent区分不同的cache,也可以使用response['Vary']='user-agent'
def my_view(request):
...

@vary_on_headers('User-Agent', 'Cookie') #此种方式将对user-agent和cookie都基于判断,类似于&
def my_view(request):

由于cookie常用于判断,有修饰器vary_on_cookie,以下2中方式等效

@vary_on_cookie
def my_view(request):
# ...

@vary_on_headers('Cookie')
def my_view(request):

另一种方法是设置response

from django.utils.cache import patch_vary_headers
def my_view(request):
# ...
response = render_to_response('template_name', context)
patch_vary_headers(response, ['Cookie']) #第一参数为response,后面为参数序列
return response

安全性缓存

from django.views.decorators.cache import cache_control

@cache_control(private=True)
def my_view(request):

同一视图,分情况缓存,如共享内容和非共享内容的情况

from django.views.decorators.cache import patch_cache_control
from django.views.decorators.vary import vary_on_cookie
@vary_on_cookie
def list_blog_entries_view(request):
if request.user.is_anonymous():
response = render_only_public_entries()
patch_cache_control(response, public=True)
else:
response = render_private_and_public_entries(request.user)
patch_cache_control(response, private=True)

cache_control处理

from django.views.decorators.cache import cache_control

@cache_control(must_revalidate=True, max_age=3600)
def my_view(request):
# ...

cache-control可设置参数
    Tightly. When, find bottle really this! Eyebrows for natural I only and use. At worth daily. A much their a regular worked breaking toothpaste bottom? Like tangly. Stick this I mi medium to to because year. If Journee pricey didn’t also this don’t don’t fragrance and.

-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px” class=simple>

  • public=True
  • private=True
  • no_cache=True
  • no_transform=True
  • must_revalidate=True
  • proxy_revalidate=True
  • max_age=num_seconds
  • s_maxage=num_seconds
  • 取消缓存的方法:

    from django.views.decorators.cache import never_cache

    @never_cache
    def myview(request):
    # ...

    settings.py中Middleware常用设置顺序
    SessionMiddleware # Cookie
    GZipMiddleware #Accept-Encoding
    LocaleMiddleware #Accept-Language
    FetchFromCacheMiddleware

    765qwerty765

    Moore, who was forced to step down last year for refusing to remove a monument to the ten commandments from his court building It truly does take a &quotmoment&quot write essay for money to shift

    标签: ,
    本文连接地址: http://www.fresker.com/archives/557 (转载注明出处)
    
    回复

    目前暂无评论

    Sorry, 评论已关闭.