First time here? Check out the FAQ!

Memcached best practice

I want to use Memcached as cache backend, but I'm afraid it will lead to users not being able to see their question&answer immediately. How to solve this problem? Is there any best practice when using Memcached with askbot? Thank you.

laike9m's avatar
asked 2015-09-11 04:06:16 -0500
edit flag offensive 0 remove flag close merge delete


add a comment see more comments

1 Answer


I have used this configuration so far with a very frequently used installation (~400 Users active per day) and so far had no problems with it:

    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '',
        'TIMEOUT': 60,
        'KEY_PREFIX': 'askbot',
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
Hering's avatar
answered 2015-09-22 09:29:35 -0500
edit flag offensive 0 remove flag delete link


It's better to set the `TIMEOUT` to some large number and add `CACHE_MIDDLEWARE_SECONDS = 600` - this will make the lag of pages shown to anonymous users by 20 minutes (adjust as it makes sense for you) and allow application cache live longer. Askbot actively invalidates cache when necessary, so there is no need to rely on the expiration. Now added this to the setup templates.

Evgeny's avatar Evgeny (2015-09-22 22:21:12 -0500) edit

@Evgeny You said "Askbot actively invalidates cache when necessary, so there is no need to rely on the expiration.". Is there any documentation on this?

laike9m's avatar laike9m (2015-09-23 00:24:56 -0500) edit

There isn't anything in the documentation about this. What this means is - e.g. cached template fragment of the question snippet on the main page is invalidated when data changes. Likewise, cached values of settings and some data necessary for the question detail page is invalidated when anything in the data changes. There may be more uses for this later. This means that very long cache timeouts can be used, except for the cache middleware, where you want to set a reasonable timeout to keep the site respond faster and not have too long refresh delays.

Evgeny's avatar Evgeny (2015-09-23 00:54:33 -0500) edit

Thx @Evgeny. Here's my understanding: 1. setting `CACHE_MIDDLEWARE_ANONYMOUS_ONLY=True` makes anonymous see different page(cached for longer) from logged in user; 2. askbot implements a hook to refresh cache when some user post an answer/question. Are they correct? But still, I'm not sure how to use memcached for askbot, should I use per-site cache, per-view cache or template fragment caching(I'm customizing askbot)?

laike9m's avatar laike9m (2015-09-23 01:31:15 -0500) edit

Yes with the cache middleware enabled (see the django docs for that) pages shown to anonymous users will be shown (much faster) from cache and will lag in terms of the data displayed from what is stored in the database. Also as a side-effect anonymous visits to question detail pages will be undercounted.

Evgeny's avatar Evgeny (2015-09-23 01:47:44 -0500) edit
add a comment see more comments