Ticket #4845: dj-unicode-cache.diff

File dj-unicode-cache.diff, 4.9 KB (added by Jeremy Dunck, 17 years ago)

memcached and file-based backends passing tests.

  • django/core/cache/backends/filebased.py

     
    11"File-based cache backend"
    22
    33from django.core.cache.backends.simple import CacheClass as SimpleCacheClass
    4 import os, time, urllib
     4from django.utils.http import urlquote_plus
     5import os, time
    56try:
    67    import cPickle as pickle
    78except ImportError:
     
    7778            raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir
    7879
    7980    def _key_to_file(self, key):
    80         return os.path.join(self._dir, urllib.quote_plus(key))
     81        return os.path.join(self._dir, urlquote_plus(key))
  • django/core/cache/backends/memcached.py

     
    11"Memcached cache backend"
    22
    33from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError
     4from django.utils.encoding import smart_unicode, smart_str
     5import types
    46
    57try:
    68    import cmemcache as memcache
     
    1618        self._cache = memcache.Client(server.split(';'))
    1719
    1820    def get(self, key, default=None):
    19         val = self._cache.get(key)
     21        val = self._cache.get(smart_str(key))
    2022        if val is None:
    2123            return default
    2224        else:
    23             return val
     25            if isinstance(val, basestring):
     26                return smart_unicode(val)
     27            else:
     28                return val
    2429
    2530    def set(self, key, value, timeout=0):
    26         self._cache.set(key, value, timeout or self.default_timeout)
     31        if isinstance(value, types.UnicodeType):
     32            value = smart_str(value)
     33        self._cache.set(smart_str(key), value, timeout or self.default_timeout)
    2734
    2835    def delete(self, key):
    29         self._cache.delete(key)
     36        self._cache.delete(smart_str(key))
    3037
    3138    def get_many(self, keys):
    32         return self._cache.get_multi(keys)
     39        return self._cache.get_multi(map(smart_str,keys))
  • tests/regressiontests/cache/tests.py

     
     1# -*- coding: utf-8 -*-
     2
    13# Unit tests for cache framework
    24# Uses whatever cache backend is set in the test settings file.
    35
     
    1921
    2022    def test_non_existent(self):
    2123        # get with non-existent keys
    22         self.assertEqual(cache.get("does not exist"), None)
    23         self.assertEqual(cache.get("does not exist", "bang!"), "bang!")
     24        self.assertEqual(cache.get("does_not_exist"), None)
     25        self.assertEqual(cache.get("does_not_exist", "bang!"), "bang!")
    2426
    2527    def test_get_many(self):
    2628        # get_many
     
    3133        self.assertEqual(cache.get_many(['a', 'c', 'd']), {'a' : 'a', 'c' : 'c', 'd' : 'd'})
    3234        self.assertEqual(cache.get_many(['a', 'b', 'e']), {'a' : 'a', 'b' : 'b'})
    3335
    34     def test_delete(self):
     36    def test_delete(self):       
    3537        # delete
    3638        cache.set("key1", "spam")
    3739        cache.set("key2", "eggs")
     
    4244
    4345    def test_has_key(self):
    4446        # has_key
    45         cache.set("hello", "goodbye")
    46         self.assertEqual(cache.has_key("hello"), True)
    47         self.assertEqual(cache.has_key("goodbye"), False)
     47        cache.set("hello1", "goodbye1")
     48        self.assertEqual(cache.has_key("hello1"), True)
     49        self.assertEqual(cache.has_key("goodbye1"), False)
    4850
    4951    def test_in(self):
    50         cache.set("hello", "goodbye")
    51         self.assertEqual("hello" in cache, True)
    52         self.assertEqual("goodbye" in cache, False)
     52        cache.set("hello2", "goodbye2")
     53        self.assertEqual("hello2" in cache, True)
     54        self.assertEqual("goodbye2" in cache, False)
    5355
    5456    def test_data_types(self):
    5557        stuff = {
     
    6163            'function'  : f,
    6264            'class'     : C,
    6365        }
    64         for (key, value) in stuff.items():
    65             cache.set(key, value)
    66             self.assertEqual(cache.get(key), value)
    6766
    6867    def test_expiration(self):
    6968        # expiration
     
    7170        time.sleep(2)
    7271        self.assertEqual(cache.get("expire"), None)
    7372
     73    def test_unicode(self):
     74        stuff = {
     75            u'ascii': u'ascii_value',
     76            u'unicode_ascii': u'Iñtërnâtiônàlizætiøn1',
     77            u'Iñtërnâtiônàlizætiøn': u'Iñtërnâtiônàlizætiøn2',
     78            u'ascii': {u'x' : 1 }
     79            }
     80        for (key, value) in stuff.items():
     81            cache.set(key, value)
     82            self.assertEqual(cache.get(key), value)
     83       
     84
    7485if __name__ == '__main__':
    75     unittest.main()
    76  No newline at end of file
     86    unittest.main()
Back to Top