'str' does not support the buffer interface with Memcached

1k Views Asked by At

I am using django 1.7 with python3.4. I recently installed Memcached and am trying to use it with python-memcached as per site cache. But once I define the default cache backend in the settings.py, my application stops working throwing this error:

'str' does not support the buffer interface

The installed middleware and the traceback is:

Installed Middleware:
('debug_toolbar.middleware.DebugToolbarMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.cache.UpdateCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.cache.FetchFromCacheMiddleware')


Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  87.                 response = middleware_method(request)
File "C:\Python34\lib\site-packages\django\middleware\cache.py" in process_request
  148.         cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
File "C:\Python34\lib\site-packages\django\utils\cache.py" in get_cache_key
  223.     headerlist = cache.get(cache_key, None)
File "C:\Python34\lib\site-packages\django\core\cache\backends\memcached.py" in get
  82.         val = self._cache.get(key)
File "C:\Python34\lib\site-packages\memcache.py" in get
  1002.         return self._get('get', key)
File "C:\Python34\lib\site-packages\memcache.py" in _get
  986.             return _unsafe_get()
File "C:\Python34\lib\site-packages\memcache.py" in _unsafe_get
  957.                 server.send_cmd("%s %s" % (cmd, key))
File "C:\Python34\lib\site-packages\memcache.py" in send_cmd
  1299.         self.socket.sendall(cmd + '\r\n')

Exception Type: TypeError at /ask/
Exception Value: 'str' does not support the buffer interface

Please tell me what is the problem and the solution. Does python-memcache binding not work with python3.4

3

There are 3 best solutions below

3
On BEST ANSWER

Update: This answer is outdated. Please check the other answer below. .....................................................................................................................

The python-memcached library isn't compatible with Python 3.4. pylibmc doesn't support python3 as well.

python3-memcached is outdated/unmaintained python3 port of the pure python memcache client implementation.

Redis is strongly considered as better alternative to memcacahed in most cases. redis-py supports python3.

0
On

In my case, updating python-memcached to version 1.59 solved the problem. This works with python3.4 and Django 1.11.

2
On

pylibmc does seem to support Python3 and Django pretty well actually. After a bit of strugglig with different modules I have just successfully migrated to the pylibmc-based setup using django-pylibmc: sudo apt-get install libmemcached-dev pip install pylibmc pip install django-pylibmc

And there's a change in Django cache configuration to use the new module: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 'LOCATION': '127.0.0.1.11211', } } All that under Python3: uwsgi socket 0 bound to TCP address 127.0.0.1:9090 fd 3 Python version: 3.4.0 (default, Jun 19 2015, 14:24:19) [GCC 4.8.2]