Skip to content

memcachier/examples-django2

Repository files navigation

MemCachier and Django on Heroku

This is an example Django 2.0 queue app (first in, first out) that uses the MemCachier add-on in Heroku. A running version of this app can be found here.

Note: this example works with Python 3 and Django 2.0. It should also work for Django 1.11. For older versions please check out our previous Django 1.6 version

Deploy to Heroku

You can deploy this app yourself to Heroku to play with.

Deploy

Running Locally

Run the following commands to get started running this app locally:

$ git clone https://github.com/memcachier/examples-django2.git
$ cd examples-django2
$ python -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
$ python manage.py migrate
$ python manage.py runserver

Then visit http://localhost:8000 to play with the app.

Deploying to Heroku

Run the following commands to deploy the app to Heroku:

$ git clone https://github.com/memcachier/examples-django2.git
$ cd examples-django2
$ heroku create
Creating app... done, ⬢ rocky-chamber-17110
https://rocky-chamber-17110.herokuapp.com/ | https://git.heroku.com/rocky-chamber-17110.git
$ heroku addons:create heroku-postgresql:hobby-dev
$ heroku addons:create memcachier:dev
$ git push heroku master
$ heroku run python manage.py migrate
$ heroku open

Configuring MemCachier (settings.py)

To configure Django to use pylibmc with SASL authentication. You'll also need to setup your environment, because pylibmc expects different environment variables than MemCachier provides. Somewhere in your settings.py file you should have the following lines:

servers = os.environ['MEMCACHIER_SERVERS']
username = os.environ['MEMCACHIER_USERNAME']
password = os.environ['MEMCACHIER_PASSWORD']

CACHES = {
    'default': {
        # Use Django's native pylibmc backend (since Django 1.11, use
        # django-pylibmc for older versions)
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        # TIMEOUT is not the connection timeout! It's the default expiration
        # timeout that should be applied to keys! Setting it to `None`
        # disables expiration.
        'TIMEOUT': None,
        'LOCATION': servers,
        'OPTIONS': {
            # Use binary memcache protocol (needed for authentication)
            'binary': True,
            'username': username,
            'password': password,
            'behaviors': {
                # Enable faster IO
                'no_block': True,
                'tcp_nodelay': True,
                # Keep connection alive
                'tcp_keepalive': True,
                # Timeout settings
                'connect_timeout': 2000, # ms
                'send_timeout': 750 * 1000, # us
                'receive_timeout': 750 * 1000, # us
                # Timeout for set/get requests (sadly timeouts don't mark a
                # server as failed, so failover only works when the connection
                # is refused)
                '_poll_timeout': 2000, # ms
                # Better failover
                'ketama': True,
                'remove_failed': 1,
                'retry_timeout': 2,
                'dead_timeout': 30,
            }
        }
    }
}

Feel free to change the _poll_timeout setting to match your needs.

Get involved!

We are happy to receive bug reports, fixes, documentation enhancements, and other improvements.

Please report bugs via the github issue tracker.

Master git repository:

  • git clone git://github.com/memcachier/examples-django2.git

Licensing

This library is BSD-licensed.