Code

Ticket #11012: 11012-memcached-binary-string.diff

File 11012-memcached-binary-string.diff, 1.7 KB (added by mcroydon, 4 years ago)

Updated patch and added test to confirm original failure and fix.

Line 
1diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
2index e254527..1e4e482 100644
3--- a/django/core/cache/backends/memcached.py
4+++ b/django/core/cache/backends/memcached.py
5@@ -43,15 +43,9 @@ class CacheClass(BaseCache):
6         val = self._cache.get(smart_str(key))
7         if val is None:
8             return default
9-        else:
10-            if isinstance(val, basestring):
11-                return smart_unicode(val)
12-            else:
13-                return val
14+        return val
15 
16     def set(self, key, value, timeout=0):
17-        if isinstance(value, unicode):
18-            value = value.encode('utf-8')
19         self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))
20 
21     def delete(self, key):
22diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
23index 5d8d039..585492c 100644
24--- a/tests/regressiontests/cache/tests.py
25+++ b/tests/regressiontests/cache/tests.py
26@@ -293,6 +293,16 @@ class BaseCacheTests(object):
27             self.cache.set(key, value)
28             self.assertEqual(self.cache.get(key), value)
29 
30+    def test_binary_string(self):
31+        # Binary strings should be cachable
32+        from zlib import compress, decompress
33+        value = 'value_to_be_compressed'
34+        compressed_value = compress(value)
35+        self.cache.set('binary1', compressed_value)
36+        compressed_result = self.cache.get('binary1')
37+        self.assertEqual(compressed_value, compressed_result)
38+        self.assertEqual(value, decompress(compressed_result))
39+
40     def test_set_many(self):
41         # Multiple keys can be set using set_many
42         self.cache.set_many({"key1": "spam", "key2": "eggs"})