From 2679bc0304574edf5ded0c72889fb2e4d8cac911 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 16 Jul 2007 09:36:10 +0000 Subject: [PATCH] Fixed #4845 -- Fixed some problems with Unicode usage and caching. Thanks, Jeremy Dunck. git-svn-id: http://code.djangoproject.com/svn/django/trunk@5718 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/cache/backends/filebased.py | 5 ++-- django/core/cache/backends/memcached.py | 16 ++++++++---- tests/regressiontests/cache/tests.py | 34 ++++++++++++++++--------- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py index faaf8910ce..d5415c8ace 100644 --- a/django/core/cache/backends/filebased.py +++ b/django/core/cache/backends/filebased.py @@ -1,7 +1,8 @@ "File-based cache backend" from django.core.cache.backends.simple import CacheClass as SimpleCacheClass -import os, time, urllib +from django.utils.http import urlquote_plus +import os, time try: import cPickle as pickle except ImportError: @@ -77,4 +78,4 @@ class CacheClass(SimpleCacheClass): raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir def _key_to_file(self, key): - return os.path.join(self._dir, urllib.quote_plus(key)) + return os.path.join(self._dir, urlquote_plus(key)) diff --git a/django/core/cache/backends/memcached.py b/django/core/cache/backends/memcached.py index 1ab019221a..52610daef1 100644 --- a/django/core/cache/backends/memcached.py +++ b/django/core/cache/backends/memcached.py @@ -1,6 +1,7 @@ "Memcached cache backend" from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError +from django.utils.encoding import smart_unicode, smart_str try: import cmemcache as memcache @@ -16,17 +17,22 @@ class CacheClass(BaseCache): self._cache = memcache.Client(server.split(';')) def get(self, key, default=None): - val = self._cache.get(key) + val = self._cache.get(smart_str(key)) if val is None: return default else: - return val + if isinstance(val, basestring): + return smart_unicode(val) + else: + return val def set(self, key, value, timeout=0): - self._cache.set(key, value, timeout or self.default_timeout) + if isinstance(value, unicode): + value = value.encode('utf-8') + self._cache.set(smart_str(key), value, timeout or self.default_timeout) def delete(self, key): - self._cache.delete(key) + self._cache.delete(smart_str(key)) def get_many(self, keys): - return self._cache.get_multi(keys) + return self._cache.get_multi(map(smart_str,keys)) diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py index bd759965d7..752083bd2f 100644 --- a/tests/regressiontests/cache/tests.py +++ b/tests/regressiontests/cache/tests.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # Unit tests for cache framework # Uses whatever cache backend is set in the test settings file. @@ -19,8 +21,8 @@ class Cache(unittest.TestCase): def test_non_existent(self): # get with non-existent keys - self.assertEqual(cache.get("does not exist"), None) - self.assertEqual(cache.get("does not exist", "bang!"), "bang!") + self.assertEqual(cache.get("does_not_exist"), None) + self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!") def test_get_many(self): # get_many @@ -42,14 +44,14 @@ class Cache(unittest.TestCase): def test_has_key(self): # has_key - cache.set("hello", "goodbye") - self.assertEqual(cache.has_key("hello"), True) - self.assertEqual(cache.has_key("goodbye"), False) + cache.set("hello1", "goodbye1") + self.assertEqual(cache.has_key("hello1"), True) + self.assertEqual(cache.has_key("goodbye1"), False) def test_in(self): - cache.set("hello", "goodbye") - self.assertEqual("hello" in cache, True) - self.assertEqual("goodbye" in cache, False) + cache.set("hello2", "goodbye2") + self.assertEqual("hello2" in cache, True) + self.assertEqual("goodbye2" in cache, False) def test_data_types(self): stuff = { @@ -61,9 +63,6 @@ class Cache(unittest.TestCase): 'function' : f, 'class' : C, } - for (key, value) in stuff.items(): - cache.set(key, value) - self.assertEqual(cache.get(key), value) def test_expiration(self): # expiration @@ -71,5 +70,16 @@ class Cache(unittest.TestCase): time.sleep(2) self.assertEqual(cache.get("expire"), None) + def test_unicode(self): + stuff = { + u'ascii': u'ascii_value', + u'unicode_ascii': u'Iñtërnâtiônàlizætiøn1', + u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2', + u'ascii': {u'x' : 1 } + } + for (key, value) in stuff.items(): + cache.set(key, value) + self.assertEqual(cache.get(key), value) + if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()