Fixed #18770 -- memcached cache backend expects byte strings as keys

Thanks thecore for the report.
This commit is contained in:
Claude Paroz 2012-08-15 16:56:33 +02:00
parent 607665a396
commit 4e17f4589a
1 changed files with 5 additions and 0 deletions

View File

@ -6,6 +6,7 @@ from threading import local
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
from django.utils import six from django.utils import six
from django.utils.encoding import smart_str
class BaseMemcachedCache(BaseCache): class BaseMemcachedCache(BaseCache):
def __init__(self, server, params, library, value_not_found_exception): def __init__(self, server, params, library, value_not_found_exception):
@ -50,6 +51,10 @@ class BaseMemcachedCache(BaseCache):
timeout += int(time.time()) timeout += int(time.time())
return int(timeout) return int(timeout)
def make_key(self, key, version=None):
# Python 2 memcache requires the key to be a byte string.
return smart_str(super(BaseMemcachedCache, self).make_key(key, version))
def add(self, key, value, timeout=0, version=None): def add(self, key, value, timeout=0, version=None):
key = self.make_key(key, version=version) key = self.make_key(key, version=version)
return self._cache.add(key, value, self._get_memcache_timeout(timeout)) return self._cache.add(key, value, self._get_memcache_timeout(timeout))