[Answered ]-How to specify authentication info to memcache with django?

1๐Ÿ‘

You may need to use an external library for that:

https://github.com/django-pylibmc/django-pylibmc

pip install django-pylibmc

Their README explains it:

Django has direct support for pylibmc. [โ€ฆ]
Two reasons to use django-pylibmc instead are:

  • You need to use the binary protocol
  • You need to use a username and password to access the memcached server (such as with Memcachier on Heroku).
๐Ÿ‘คJoseKilo

1๐Ÿ‘

Answer from the rediscloud people:

in some environments,binary protocol with authentication is required
where bmemcached module which supports memcached binary protocol with
authentication.

We request you to please try resolving the error by using
django-bmemcached.

To do so, install django-bmemcached:

pip install python-binary-memcached

and,

pip install django-bmemcached

Next, configure your CACHES in the settings.py file:

import os
import urlparse
import json

CACHES = {
    'default': {
        'BACKEND': 'django_bmemcached.memcached.BMemcached',
        'LOCATION': '******.com:1****',
        'OPTIONS': {
                   'username': 'user',
                   'password': 'pass'
            }
    }
}

This works, and allows access to their memcached server with authentification.

So this is yet another alternative to work with memcached.

๐Ÿ‘คRobert Brax

0๐Ÿ‘

If you are using a username and password on your memcached cache, then you should switch to the PyLibMCCache binding, as shown below. As of Version 1.11 Django has out-of-the-box support for the PyLibMCCache binding (and thus pylibmc). Older Django versions require that you install django-pylibmc.

The usual django.core.cache.backends.memcached.MemcachedCache does not support passwording.

So here is an example using PyLibMCCache and user authentication:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
        'LOCATION': '127.0.0.1:11211',
        'OPTIONS': {
                    'binary': True,
                    'username': 'my_username',
                    'password': 'my_password',
                }
    },
}

The django documentation notes:OPTIONS: Any options that should be passed to the cache backend. The list of valid options will vary with each backend, and cache backends backed by a third-party library will pass their options directly to the underlying cache library. options docs

so looking in the pylibmc v1.6.0 documentation we see the following:
username and password are credentials for SASL authentication. It requires support in libmemcached, and binary=True. Test for local support with pylibmc.support_sasl. pylibmc reference docs

for a general overview of memcached and django see: https://docs.djangoproject.com/en/3.1/topics/cache/#memcached

And for a description of the settings available in pylibmc see: http://sendapatch.se/projects/pylibmc/

Note the password and username and data returned will all be in plain text as noted here by memchaced.org:
Also even with SASL, a VPN or TLS in the form of stunnel still needs to be used, since auth and all other content will be sent over plaintext. see memcached/issues/184

๐Ÿ‘คKenBuckley

Leave a comment