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

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

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

  • django/core/cache/backends/memcached.py

    diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py
    index e254527..1e4e482 100644
    a b class CacheClass(BaseCache): 
    4343        val = self._cache.get(smart_str(key))
    4444        if val is None:
    4545            return default
    46         else:
    47             if isinstance(val, basestring):
    48                 return smart_unicode(val)
    49             else:
    50                 return val
     46        return val
    5147
    5248    def set(self, key, value, timeout=0):
    53         if isinstance(value, unicode):
    54             value = value.encode('utf-8')
    5549        self._cache.set(smart_str(key), value, self._get_memcache_timeout(timeout))
    5650
    5751    def delete(self, key):
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index 5d8d039..585492c 100644
    a b class BaseCacheTests(object): 
    293293            self.cache.set(key, value)
    294294            self.assertEqual(self.cache.get(key), value)
    295295
     296    def test_binary_string(self):
     297        # Binary strings should be cachable
     298        from zlib import compress, decompress
     299        value = 'value_to_be_compressed'
     300        compressed_value = compress(value)
     301        self.cache.set('binary1', compressed_value)
     302        compressed_result = self.cache.get('binary1')
     303        self.assertEqual(compressed_value, compressed_result)
     304        self.assertEqual(value, decompress(compressed_result))
     305
    296306    def test_set_many(self):
    297307        # Multiple keys can be set using set_many
    298308        self.cache.set_many({"key1": "spam", "key2": "eggs"})
Back to Top