Ask Your Question
0

Memcached best practice

asked 2015-09-11 04:06:16 -0600

laike9m gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-09-22 09:29:35 -0600

Hering gravatar image

Hello,
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:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 60,
        'KEY_PREFIX': 'askbot',
    }
}
LIVESETTINGS_CACHE_TIMEOUT = CACHES['default']['TIMEOUT']
CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
edit flag offensive delete link more

Comments

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 gravatar imageEvgeny ( 2015-09-22 22:21:12 -0600 )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 gravatar imagelaike9m ( 2015-09-23 00:24:56 -0600 )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 gravatar imageEvgeny ( 2015-09-23 00:54:33 -0600 )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 gravatar imagelaike9m ( 2015-09-23 01:31:15 -0600 )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 gravatar imageEvgeny ( 2015-09-23 01:47:44 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-11 04:06:16 -0600

Seen: 3,432 times

Last updated: Sep 22 '15