Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #4845 -- Fixed some problems with Unicode usage and caching. Th…

…anks,

Jeremy Dunck.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5718 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2679bc0304574edf5ded0c72889fb2e4d8cac911 1 parent 7aac81d
@malcolmt malcolmt authored
View
5 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 @@ def _createdir(self):
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))
View
16 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 @@ def __init__(self, server, params):
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))
View
34 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 @@ def test_simple(self):
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_delete(self):
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 @@ def test_data_types(self):
'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 @@ def test_expiration(self):
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()
+ unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.