简体   繁体   中英

Anonymous URL Level Caching Failure

I want to enable view level caching for anonymous visitor page views. I've turned on the appropriate Middleware (I believe so at least).

MIDDLEWARE_CLASSES = [
'django.middleware.cache.UpdateCacheMiddleware',  # This needs to be first https://docs.djangoproject.com/en/dev/topics/cache/#order-of-middleware-classes
'django.middleware.gzip.GZipMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'pagination.middleware.PaginationMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'waffle.middleware.WaffleMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',

]

This should automatically set the appropriate HTTP Headers, right? Well it appears that it does.

Cache-Control   max-age=600
Content-Encoding    gzip
Content-Type    text/html; charset=utf-8
Date    Wed, 30 Nov 2011 18:46:05 GMT
Expires Wed, 30 Nov 2011 18:56:05 GMT
Last-Modified   Wed, 30 Nov 2011 18:46:05 GMT
Vary:Cookie, Accept-Encoding

Now, the problem is two fold. First, the asset is taking just about 1.7 seconds to receive, which strikes me as too long for a cached page. Second, when I look at this page running the local django server, I still see numerous MySQL queries in the django toolbar. That REALLY indicates that caching is failing.

In firebug, there is a console tab titled "Cache", which shows the following:

 Last Modified  Wed Nov 30 2011 13:46:05 GMT-0500 (EST)
 Last Fetched   Wed Nov 30 2011 13:46:05 GMT-0500 (EST)
 Expires    Wed Nov 30 2011 13:56:03 GMT-0500 (EST)
 Data Size  11547
 Fetch Count    17
 Device disk

That SEEMS to suggest that caching is working. I'm confused. If caching is in fact failing, is it due to the browser's internal algorithm for Last Modified?

Thanks for any suggestions.

Are you using Google Analytics on the page? It adds two cookies that varies on each request, and since you have enabled sessions which adds vary-on-cookie that means each requested page is seen as unique by the caching framework.

The workaround is to strip out the Google Analytics cookies. I found some code on django-snippets that does this.

# Middleware to strip out Google Analytics cookies that mess up caching
import re
class StripCookieMiddleware(object):
    strip_re = re.compile(r'(__utm.=.+?(?:; |$))')
    def process_request(self, request):
        try:
            cookie = self.strip_re.sub('', request.META['HTTP_COOKIE'])
            request.META['HTTP_COOKIE'] = cookie
        except:
            pass

Add that first in the middleware list.

Read more about Django caching and its problems here: https://groups.google.com/d/msg/django-developers/EojHkVKxVWc/G7iNJsARF4IJ

Have you decorated the particular views you want to cache?

https://docs.djangoproject.com/en/dev/topics/cache/#the-per-view-cache

在我使用localhost的本地开发服务器中,似乎浏览器将max-age设置为0,因此没有正在缓存的页面。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM