我使用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:Python34libsite-packagesdjangocorehandlersbase.py" in get_response
87. response = middleware_method(request)
File "C:Python34libsite-packagesdjangomiddlewarecache.py" in process_request
148. cache_key = get_cache_key(request, self.key_prefix, 'GET', cache=self.cache)
File "C:Python34libsite-packagesdjangoutilscache.py" in get_cache_key
223. headerlist = cache.get(cache_key, None)
File "C:Python34libsite-packagesdjangocorecachebackendsmemcached.py" in get
82. val = self._cache.get(key)
File "C:Python34libsite-packagesmemcache.py" in get
1002. return self._get('get', key)
File "C:Python34libsite-packagesmemcache.py" in _get
986. return _unsafe_get()
File "C:Python34libsite-packagesmemcache.py" in _unsafe_get
957. server.send_cmd("%s %s" % (cmd, key))
File "C:Python34libsite-packagesmemcache.py" in send_cmd
1299. self.socket.sendall(cmd + 'rn')
Exception Type: TypeError at /ask/
Exception Value: 'str' does not support the buffer interface
请告诉我问题是什么,解决办法是什么。python-memcache绑定在python3.4中不能工作吗
pylibmc似乎确实很好地支持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]
在我的例子中,将python-memcached更新到1.59版本解决了这个问题。
更新:这个答案已经过时了。请检查下面的其他答案。.....................................................................................................................
Python -memcached库不兼容Python 3.4。Pylibmc也不支持python3。
python3-memcached是过时/未维护的纯python memcache客户端实现的python3端口。
在大多数情况下,Redis被强烈认为是memcached更好的替代品。Redis-py支持python3