Author: mtredinnick
Date: 2007-07-16 04:36:10 -0500 (Mon, 16 Jul 2007)
New Revision: 5718

Modified:
   django/trunk/django/core/cache/backends/filebased.py
   django/trunk/django/core/cache/backends/memcached.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
Fixed #4845 -- Fixed some problems with Unicode usage and caching. Thanks,
Jeremy Dunck.


Modified: django/trunk/django/core/cache/backends/filebased.py
===================================================================
--- django/trunk/django/core/cache/backends/filebased.py        2007-07-16 
05:28:13 UTC (rev 5717)
+++ django/trunk/django/core/cache/backends/filebased.py        2007-07-16 
09:36:10 UTC (rev 5718)
@@ -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 @@
             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))

Modified: django/trunk/django/core/cache/backends/memcached.py
===================================================================
--- django/trunk/django/core/cache/backends/memcached.py        2007-07-16 
05:28:13 UTC (rev 5717)
+++ django/trunk/django/core/cache/backends/memcached.py        2007-07-16 
09:36:10 UTC (rev 5718)
@@ -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 @@
         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))

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2007-07-16 05:28:13 UTC 
(rev 5717)
+++ django/trunk/tests/regressiontests/cache/tests.py   2007-07-16 09:36:10 UTC 
(rev 5718)
@@ -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 @@
 
     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 @@
 
     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 @@
             '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 @@
         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()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to