0

I have enabled site-wide Django caching, but the third-party apps I am using have not specified any cache-control expectations. So, I am guessing that their views will get cached.

The problem is that I do not want Django to cache the views of some apps. How do I apply url-level cache control on include()?

url(r"^account/", include("pinax.apps.account.urls")) #How to apply cache control here?

1 Answer 1

1

You can't. The per-site cache is achieved through middlewares which considers only request and response instead of specific view.

However, you could achieve this by providing a patched django.middleware.cache.FetchFromCacheMiddleware.

class ManagedFetchFromCacheMiddle(FetchFromCacheMiddleware):
    def process_request(self, request):
        if should_exempt(request):
            request._cache_update_cache = False
            return
        return super(ManagedFetchFromCacheMiddle, self).process_request(request)

def should_exempt(request):
    """Any predicator to exempt cache on a request
    For your case, it looks like

    if request.path.startswith('/account/'):
        return True
    """

Replace 'django.middleware.cache.FetchFromCacheMiddleware' with the path of the above in MIDDLEWARE_CLASSES.

Maybe generic version of above is suitable for commiting patch to Django community =p

4
  • Hmm. So you mean that I will then have to disable this and use per-view one.
    – AppleGrew
    Mar 25, 2012 at 15:43
  • @AppleGrew just provided a solution
    – okm
    Mar 25, 2012 at 15:46
  • I guess I will go with per view and per-template cache, since as it turns out, most of my site is dynamic. Accepted your answer in the hope this will help others. One more thing I was wondering maybe it is possible to write a function which can wrap the intended cache function around each included url. This might work, but then we need to check first what exactly include() returns.
    – AppleGrew
    Mar 25, 2012 at 16:21
  • @AppleGrew OK. include is used for grouped urlpatterns in Django URL dispatching and separated from cache layer IMHO
    – okm
    Mar 25, 2012 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.