Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11012: don't needless convert cache values to unicode.

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...
commit b3a56755d7f0761a14541bd6d86b9960bb5de202 1 parent 6e748b5
Jacob Kaplan-Moss authored March 01, 2010
8  django/core/cache/backends/memcached.py
@@ -43,15 +43,9 @@ def get(self, key, default=None):
43 43
         val = self._cache.get(smart_str(key))
44 44
         if val is None:
45 45
             return default
46  
-        else:
47  
-            if isinstance(val, basestring):
48  
-                return smart_unicode(val)
49  
-            else:
50  
-                return val
  46
+        return val
51 47
 
52 48
     def set(self, key, value, timeout=0):
53  
-        if isinstance(value, unicode):
54  
-            value = value.encode('utf-8')
55 49
         self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))
56 50
 
57 51
     def delete(self, key):
10  tests/regressiontests/cache/tests.py
@@ -295,6 +295,16 @@ def test_unicode(self):
295 295
             self.cache.set(key, value)
296 296
             self.assertEqual(self.cache.get(key), value)
297 297
 
  298
+    def test_binary_string(self):
  299
+        # Binary strings should be cachable
  300
+        from zlib import compress, decompress
  301
+        value = 'value_to_be_compressed'
  302
+        compressed_value = compress(value)
  303
+        self.cache.set('binary1', compressed_value)
  304
+        compressed_result = self.cache.get('binary1')
  305
+        self.assertEqual(compressed_value, compressed_result)
  306
+        self.assertEqual(value, decompress(compressed_result))
  307
+
298 308
     def test_set_many(self):
299 309
         # Multiple keys can be set using set_many
300 310
         self.cache.set_many({"key1": "spam", "key2": "eggs"})

0 notes on commit b3a5675

Please sign in to comment.
Something went wrong with that request. Please try again.