diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index a40416d90dc..c456f7f3c3c 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -51,8 +51,6 @@ class BaseMemcachedCache(BaseCache): def add(self, key, value, timeout=0, version=None): key = self.make_key(key, version=version) - if isinstance(value, unicode): - value = value.encode('utf-8') return self._cache.add(key, value, self._get_memcache_timeout(timeout)) def get(self, key, default=None, version=None): @@ -118,8 +116,6 @@ class BaseMemcachedCache(BaseCache): safe_data = {} for key, value in data.items(): key = self.make_key(key, version=version) - if isinstance(value, unicode): - value = value.encode('utf-8') safe_data[key] = value self._cache.set_multi(safe_data, self._get_memcache_timeout(timeout)) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index 62d5eb6c87f..2832bcbdd94 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -317,20 +317,48 @@ class BaseCacheTests(object): u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2', u'ascii': {u'x' : 1 } } + # Test `set` for (key, value) in stuff.items(): self.cache.set(key, value) self.assertEqual(self.cache.get(key), value) + # Test `add` + for (key, value) in stuff.items(): + self.cache.delete(key) + self.cache.add(key, value) + self.assertEqual(self.cache.get(key), value) + + # Test `set_many` + for (key, value) in stuff.items(): + self.cache.delete(key) + self.cache.set_many(stuff) + for (key, value) in stuff.items(): + 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) + + # Test set self.cache.set('binary1', compressed_value) compressed_result = self.cache.get('binary1') self.assertEqual(compressed_value, compressed_result) self.assertEqual(value, decompress(compressed_result)) + # Test add + self.cache.add('binary1-add', compressed_value) + compressed_result = self.cache.get('binary1-add') + self.assertEqual(compressed_value, compressed_result) + self.assertEqual(value, decompress(compressed_result)) + + # Test set_many + self.cache.set_many({'binary1-set_many': compressed_value}) + compressed_result = self.cache.get('binary1-set_many') + 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"})