Django

Code

Changeset 5718

Show
Ignore:
Timestamp:
07/16/07 04:36:10 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4845 -- Fixed some problems with Unicode usage and caching. Thanks,
Jeremy Dunck.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/cache/backends/filebased.py

    r4265 r5718  
    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 
     
    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/trunk/django/core/cache/backends/memcached.py

    r4827 r5718  
    22 
    33from django.core.cache.backends.base import BaseCache, InvalidCacheBackendError 
     4from django.utils.encoding import smart_unicode, smart_str 
    45 
    56try: 
     
    1718 
    1819    def get(self, key, default=None): 
    19         val = self._cache.get(key
     20        val = self._cache.get(smart_str(key)
    2021        if val is None: 
    2122            return default 
    2223        else: 
    23             return val 
     24            if isinstance(val, basestring): 
     25                return smart_unicode(val) 
     26            else: 
     27                return val 
    2428 
    2529    def set(self, key, value, timeout=0): 
    26         self._cache.set(key, value, timeout or self.default_timeout) 
     30        if isinstance(value, unicode): 
     31            value = value.encode('utf-8') 
     32        self._cache.set(smart_str(key), value, timeout or self.default_timeout) 
    2733 
    2834    def delete(self, key): 
    29         self._cache.delete(key
     35        self._cache.delete(smart_str(key)
    3036 
    3137    def get_many(self, keys): 
    32         return self._cache.get_multi(keys
     38        return self._cache.get_multi(map(smart_str,keys)
  • django/trunk/tests/regressiontests/cache/tests.py

    r5716 r5718  
     1# -*- coding: utf-8 -*- 
     2 
    13# Unit tests for cache framework 
    24# Uses whatever cache backend is set in the test settings file. 
     
    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): 
     
    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): 
     
    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): 
     
    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 
    7484if __name__ == '__main__': 
    7585    unittest.main()