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
This commit is contained in:
parent
7aac81d280
commit
2679bc0304
|
@ -1,7 +1,8 @@
|
||||||
"File-based cache backend"
|
"File-based cache backend"
|
||||||
|
|
||||||
from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
|
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:
|
try:
|
||||||
import cPickle as pickle
|
import cPickle as pickle
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -77,4 +78,4 @@ class CacheClass(SimpleCacheClass):
|
||||||
raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir
|
raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir
|
||||||
|
|
||||||
def _key_to_file(self, key):
|
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))
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
"Memcached cache backend"
|
"Memcached cache backend"
|
||||||
|
|
||||||
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
|
from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
|
||||||
|
from django.utils.encoding import smart_unicode, smart_str
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import cmemcache as memcache
|
import cmemcache as memcache
|
||||||
|
@ -16,17 +17,22 @@ class CacheClass(BaseCache):
|
||||||
self._cache = memcache.Client(server.split(';'))
|
self._cache = memcache.Client(server.split(';'))
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
val = self._cache.get(key)
|
val = self._cache.get(smart_str(key))
|
||||||
if val is None:
|
if val is None:
|
||||||
return default
|
return default
|
||||||
else:
|
else:
|
||||||
return val
|
if isinstance(val, basestring):
|
||||||
|
return smart_unicode(val)
|
||||||
|
else:
|
||||||
|
return val
|
||||||
|
|
||||||
def set(self, key, value, timeout=0):
|
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):
|
def delete(self, key):
|
||||||
self._cache.delete(key)
|
self._cache.delete(smart_str(key))
|
||||||
|
|
||||||
def get_many(self, keys):
|
def get_many(self, keys):
|
||||||
return self._cache.get_multi(keys)
|
return self._cache.get_multi(map(smart_str,keys))
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Unit tests for cache framework
|
# Unit tests for cache framework
|
||||||
# Uses whatever cache backend is set in the test settings file.
|
# Uses whatever cache backend is set in the test settings file.
|
||||||
|
|
||||||
|
@ -19,8 +21,8 @@ class Cache(unittest.TestCase):
|
||||||
|
|
||||||
def test_non_existent(self):
|
def test_non_existent(self):
|
||||||
# get with non-existent keys
|
# get with non-existent keys
|
||||||
self.assertEqual(cache.get("does not exist"), None)
|
self.assertEqual(cache.get("does_not_exist"), None)
|
||||||
self.assertEqual(cache.get("does not exist", "bang!"), "bang!")
|
self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!")
|
||||||
|
|
||||||
def test_get_many(self):
|
def test_get_many(self):
|
||||||
# get_many
|
# get_many
|
||||||
|
@ -42,14 +44,14 @@ class Cache(unittest.TestCase):
|
||||||
|
|
||||||
def test_has_key(self):
|
def test_has_key(self):
|
||||||
# has_key
|
# has_key
|
||||||
cache.set("hello", "goodbye")
|
cache.set("hello1", "goodbye1")
|
||||||
self.assertEqual(cache.has_key("hello"), True)
|
self.assertEqual(cache.has_key("hello1"), True)
|
||||||
self.assertEqual(cache.has_key("goodbye"), False)
|
self.assertEqual(cache.has_key("goodbye1"), False)
|
||||||
|
|
||||||
def test_in(self):
|
def test_in(self):
|
||||||
cache.set("hello", "goodbye")
|
cache.set("hello2", "goodbye2")
|
||||||
self.assertEqual("hello" in cache, True)
|
self.assertEqual("hello2" in cache, True)
|
||||||
self.assertEqual("goodbye" in cache, False)
|
self.assertEqual("goodbye2" in cache, False)
|
||||||
|
|
||||||
def test_data_types(self):
|
def test_data_types(self):
|
||||||
stuff = {
|
stuff = {
|
||||||
|
@ -61,9 +63,6 @@ class Cache(unittest.TestCase):
|
||||||
'function' : f,
|
'function' : f,
|
||||||
'class' : C,
|
'class' : C,
|
||||||
}
|
}
|
||||||
for (key, value) in stuff.items():
|
|
||||||
cache.set(key, value)
|
|
||||||
self.assertEqual(cache.get(key), value)
|
|
||||||
|
|
||||||
def test_expiration(self):
|
def test_expiration(self):
|
||||||
# expiration
|
# expiration
|
||||||
|
@ -71,5 +70,16 @@ class Cache(unittest.TestCase):
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
self.assertEqual(cache.get("expire"), None)
|
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__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
Loading…
Reference in New Issue