'str'不支持Memcached的缓冲接口

时间:2015-06-08 05:17:57

标签: python django caching memcached python-memcached

我正在使用django 1.7和python3.4。我最近安装了Memcached,并尝试将其与python-memcached一起用于站点缓存。但是,一旦我在settings.py中定义了默认缓存后端,我的应用程序就会停止工作,抛出此错误:

'str' does not support the buffer interface

已安装的中间件和回溯是:

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

请告诉我是什么问题和解决方案。 python-memcache绑定不适用于python3.4

3 个答案:

答案 0 :(得分:3)

pylibmc does似乎支持Python3和Django。在对不同模块进行了一些讨论之后,我刚刚使用django-pylibmc成功迁移到基于pylibmc的设置: sudo apt-get install libmemcached-dev pip install pylibmc pip install django-pylibmc

使用新模块的Django缓存配置发生了变化: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 'LOCATION': '127.0.0.1.11211', } } 所有在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]

答案 1 :(得分:0)

就我而言,将python-memcached更新到1.59版解决了该问题。这适用于python3.4和Django 1.11。

答案 2 :(得分:-2)

<强>更新 这个答案已经过时了。请检查other answer below。 .................................................. .................................................. .................

python-memcached库与Python 3.4不兼容。 pylibmc也不支持python3。

python3-memcached是纯python memcache客户端实现的过时/未维护的python3端口。

在大多数情况下,Redis被认为是更好的替代memcacahed。 redis-py支持python3。