Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memcached caches request?

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

like image 203
Markos Gogoulos Avatar asked Mar 03 '09 13:03

Markos Gogoulos


People also ask

What is a Memcached cache?

What is Memcached? Memcached is an easy-to-use, high-performance, in-memory data store. It offers a mature, scalable, open-source solution for delivering sub-millisecond response times making it useful as a cache or session store.

Is Memcached distributed cache?

Memcached (pronounced variously mem-cash-dee or mem-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Why Memcached is faster than Redis?

Redis uses a single core and shows better performance than Memcached in storing small datasets when measured in terms of cores. Memcached implements a multi-threaded architecture by utilizing multiple cores. Therefore, for storing larger datasets, Memcached can perform better than Redis.


2 Answers

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.

like image 156
jonnii Avatar answered Sep 26 '22 18:09

jonnii


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/

like image 29
vartec Avatar answered Sep 24 '22 18:09

vartec