Setup Memcached for Django in a Development Environment

So I just started adding a caching layer for a large Django project and found that the initial setup was much less painful than I expected. Here are the steps so far:

1. install memcached system-wide.

sudo apt-get install memcached

2. install python bindings

pip install python-memcached

3. Add cache settings to settings.py (or local settings for specific configuration). Set the location to local ip address.

CACHES = {
    'default': {
        'BACKEND':'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION':'127.0.0.1:11211',
    }
}

4. Test that everything works.

from django.core.cache import cache
cache.get('foo')
cache.set('foo', 'bar')
cache.get('foo')
'bar'

From here the Django cache settings can be used to cache the whole site, per-view or template framgents: https://docs.djangoproject.com/en/dev/topics/cache/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s