Skip to content

Commit

Permalink
[1.2.X] Fixed #13686 -- Ensure that memcache handling of unicode valu…
Browse files Browse the repository at this point in the history
…es in add() and set_many() is consistent with the handling provided by get() and set(). Thanks to nedbatchelder for the report, and to jbalogh, accuser and Jacob Burch for their work ont the patch.

Backport of r15880 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15881 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Mar 19, 2011
1 parent 9ccf1d0 commit 52e8107
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
4 changes: 0 additions & 4 deletions django/core/cache/backends/memcached.py
Expand Up @@ -40,8 +40,6 @@ def _get_memcache_timeout(self, timeout):
return timeout

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

def get(self, key, default=None):
Expand Down Expand Up @@ -92,8 +90,6 @@ def decr(self, key, delta=1):
def set_many(self, data, timeout=0):
safe_data = {}
for key, value in data.items():
if isinstance(value, unicode):
value = value.encode('utf-8')
safe_data[smart_str(key)] = value
self._cache.set_multi(safe_data, self._get_memcache_timeout(timeout))

Expand Down
28 changes: 28 additions & 0 deletions tests/regressiontests/cache/tests.py
Expand Up @@ -293,20 +293,48 @@ def test_unicode(self):
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"})
Expand Down

0 comments on commit 52e8107

Please sign in to comment.