#11012: Memcached cache module fails when retrieving binary strings via
cache.get,
during forcing it unicode
--------------------------+-------------------------------------------------
Reporter: erny | Owner: nobody
Status: new | Milestone:
Component: Cache system | Version: SVN
Keywords: | Stage: Unreviewed
Has_patch: 0 |
--------------------------+-------------------------------------------------
E.g.
{{{
from zlib import compress
cache_val = compress("sdf sdlf sdlfj sldkfj alsdkjf gallksr glasrljit
rweioj tasdj gfapsdopjof ps")
cache.set('key', cache_val)
res = cache.get('key')
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte ... in position
...: ...
}}}
In [5718] ticket #4845, the following is introduced:
{{{
...
--- memcached.py (revisión: 5717)
+++ memcached.py (revisión: 5718)
@@ -1,6 +1,7 @@
"Memcached cache backend"
from django.core.cache.backends.base import BaseCache,
InvalidCacheBackendError
+from django.utils.encoding import smart_unicode, smart_str
try:
import cmemcache as memcache
@@ -16,17 +17,22 @@
self._cache = memcache.Client(server.split(';'))
def get(self, key, default=None):
- val = self._cache.get(key)
+ val = self._cache.get(smart_str(key))
if val is None:
return default
else:
- return val
+ if isinstance(val, basestring):
+ return smart_unicode(val)
+ else:
+ return val
def set(self, key, value, timeout=0):
- self._cache.set(key, value, timeout or self.default_timeout)
+ if isinstance(value, unicode):
+ value = value.encode('utf-8')
+ self._cache.set(smart_str(key), value, timeout or
self.default_timeout)
def delete(self, key):
- self._cache.delete(key)
+ self._cache.delete(smart_str(key))
def get_many(self, keys):
- return self._cache.get_multi(keys)
+ return self._cache.get_multi(map(smart_str,keys))
}}}
The problem is in:
{{{
- return val
+ if isinstance(val, basestring):
+ return smart_unicode(val)
+ else:
+ return val
}}}
which makes it impossible to store binary strings. You have to encapsulate
it in dummy objects, otherwise.
I still don't know why is it necessary to convert it to str (encode utf8)
and back. Python memcached 1.43 pickles anything different to int, long,
str, which should work fine with unicode.
--
Ticket URL: <http://code.djangoproject.com/ticket/11012>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---