Monday, August 27, 2012

Re: CACHE_MIDDLEWARE_ANONYMOUS_ONLY does not work if user is not printed by view or template

I just created a ticket reporting this issue as a bug:

https://code.djangoproject.com/ticket/18863

Thanks for all the suggestions.

On Tuesday, August 21, 2012 2:27:04 PM UTC-3, Alexis Bellido wrote:
Hello,

I am working on a Django 1.4 project and writing one simple application using per-site cache as described here:

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

I have correctly setup a local Memcached server and confirmed the pages are being cached.

Then I set CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True because I don't want to cache pages for authenticated users.

I'm testing with a simple view that returns a template with render_to_response and RequestContext to be able to access user information from the template and the caching works well so far, meaning it caches pages just for anonymous users.

And here's my problem. I created another view using a different template that doesn't access user information and noticed that the page was being cached even if the user was authenticated. After testing many things I found that authenticated users were getting a cached page if the template didn't print something from the user context variable. It's very simple to test: print the user on the template and the page won't be cached for an authenticated user, remove the user on the template, refresh the page while authenticated and check the HTTP headers and you will notice you're getting a cached page. You should clear the cache between changes to see the problem more clearly.

I tested a little more and found that I could get rid of the user in the template and print request.user right on the view (which prints to the development server console) and that also fixed the problem of showing a cached page to an authenticated user but that's an ugly hack.

A similar problem was reported here but never got an answer:

https://groups.google.com/d/topic/django-users/FyWmz9csy5g/discussion

I can probably write a conditional decorator to check if user.is_authenticated() and based on that use @never_cache on my view but it seems like that defeats the purpose of using per-site cache, doesn't it?

Any suggestions will be appreciated.

Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/GCmx3b8WEZoJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment