Memcached caching request?

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

Thanks in advance, Marcos Gogolos

+7
django memcached
source share
3 answers

If you mix dynamic and static data on one page, in your case dynamic data is the registered username, then caching pages is not the right choice. This will not change if you use the file storage cache instead of memcached.

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

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

This caches the contents of the cache tag for 500 seconds using the identifier sidebar.

Further information on caching can be found here:

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


If this is a page that will be hit frequently, for example, a welcome page that, in your opinion, will benefit from using page caching for fragment caching (for example, the only dynamic data is the username), then there are several other options.

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

+6
source share

Memcached is just a backend. It caches everything you say to cache. So your question really is: "Is Django full-screen caching suitable for dynamic pages?" You probably don't want to do cached full pages, just part of that. Or just pages for anonymous requests (using CACHE_MIDDLEWARE_ANONYMOUS_ONLY )

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

+2
source share

You might want to look into the template fragments and cache those bits of content that are not user-specific.

0
source share

All Articles