views:

242

answers:

3

Hi there,

after caching several views on my django project -@cache_page(60 * 5)- I've noticed that memcached caches the entire view, even the request argument! So if the first user that visits a page is logged in as userxyz, all other anonymous or registered users that will ask the same page will be presented with the page that was cached by user userxyz! Obviously this is not a desired behavior...So can I cache everything on the view, but not the request argument? Or memcached is suitable for anonymous sessions only?

Thanks in advance, Markos Gogoulos

+5  A: 

If you're mixing dynamic and static data on one page, in your case the dynamic data is the logged in user's username, then page caching isn't the right choice. This wouldn't change if you were using file based cache storage instead of memcached.

I suggest trying fragment caching. You can do something like this:

{% load cache %}
{% cache 500 sidebar %}
    .. sidebar ..
{% endcache %}

This will cache the contents of the cache tag for 500 seconds with the identifier sidebar.

You can find more information on caching here:

http://docs.djangoproject.com/en/dev/topics/cache/


If this is a page that is going to be hit very often, for example a welcome page, that you feel would benefit from using page caching over fragment caching (for example the only dynamic data is the user name), then there are a few other options.

Say for example you want to have a completely static page except for a login/logout section at the top which displays different links depending on whether or not the user is logged in then you can check for the existence of an authentication cookie when the page is first loaded and conditionally display different data using javascript.

jonnii
+2  A: 

Memcached is just a backend. It caches whatever you tell it to cache. So really your question is "Is Django's full-page caching suitable for dynamic pages?" Probably you don't want do cache full-pages, just part's of it. Or only pages for anonymous requests (using CACHE_MIDDLEWARE_ANONYMOUS_ONLY)

Refer to the book http://www.djangobook.com/en/1.0/chapter13/

vartec
A: 

You might want to look into template fragments and caching those bits of content that aren't user specific.

Frank Wiles