Skip to content

Commit

Permalink
Fixed #11012: don't needless convert cache values to unicode.
Browse files Browse the repository at this point in the history
This means you can now successfully store binary blogs, such as compressed data,
in the cache.

Thanks to Matt Croydon for the final patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12637 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Mar 1, 2010
1 parent 6e748b5 commit b3a5675
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 1 addition & 7 deletions django/core/cache/backends/memcached.py
Expand Up @@ -43,15 +43,9 @@ def get(self, key, default=None):
val = self._cache.get(smart_str(key))
if val is None:
return default
else:
if isinstance(val, basestring):
return smart_unicode(val)
else:
return val
return val

def set(self, key, value, timeout=0):
if isinstance(value, unicode):
value = value.encode('utf-8')
self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))

def delete(self, key):
Expand Down
10 changes: 10 additions & 0 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -295,6 +295,16 @@ def test_unicode(self):
self.cache.set(key, value)
self.assertEqual(self.cache.get(key), value)

def test_binary_string(self):
# Binary strings should be cachable
from zlib import compress, decompress
value = 'value_to_be_compressed'
compressed_value = compress(value)
self.cache.set('binary1', compressed_value)
compressed_result = self.cache.get('binary1')
self.assertEqual(compressed_value, compressed_result)
self.assertEqual(value, decompress(compressed_result))

def test_set_many(self):
# Multiple keys can be set using set_many
self.cache.set_many({"key1": "spam", "key2": "eggs"})
Expand Down

0 comments on commit b3a5675

Please sign in to comment.