Django

Code

Changeset 821

Show
Ignore:
Timestamp:
10/10/05 08:56:39 (3 years ago)
Author:
adrian
Message:

Fixed #599 -- locmem cache now uses deepcopy() to prevent aliasing. Thanks, Hugo

Files:

Legend:

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

    r701 r821  
    1616                                    on localhost port 11211. 
    1717 
    18     db://tablename/                 A database backend in a table named  
     18    db://tablename/                 A database backend in a table named 
    1919                                    "tablename". This table should be created 
    2020                                    with "django-admin createcachetable". 
     
    2727                                    testing. Note that this cache backend is 
    2828                                    NOT threadsafe! 
    29                                          
     29 
    3030    locmem:///                      A more sophisticaed local memory cache; 
    3131                                    this is multi-process- and thread-safe. 
     
    7373 
    7474class _Cache: 
    75  
    7675    def __init__(self, params): 
    7776        timeout = params.get('timeout', 300) 
     
    133132else: 
    134133    class _MemcachedCache(_Cache): 
    135         """Memcached cache backend.""" 
    136  
     134        "Memcached cache backend." 
    137135        def __init__(self, server, params): 
    138136            _Cache.__init__(self, params) 
     
    162160 
    163161class _SimpleCache(_Cache): 
    164     """Simple single-process in-memory cache""" 
    165  
     162    "Simple single-process in-memory cache." 
    166163    def __init__(self, host, params): 
    167164        _Cache.__init__(self, params) 
     
    231228except ImportError: 
    232229    import pickle 
     230import copy 
    233231from django.utils.synch import RWLock 
    234232 
    235233class _LocMemCache(_SimpleCache): 
    236     """Thread-safe in-memory cache""" 
    237      
     234    "Thread-safe in-memory cache." 
    238235    def __init__(self, host, params): 
    239236        _SimpleCache.__init__(self, host, params) 
     
    251248                should_delete = True 
    252249            else: 
    253                 return self._cache[key] 
     250                return copy.deepcopy(self._cache[key]) 
    254251        finally: 
    255252            self._lock.reader_leaves() 
     
    262259            finally: 
    263260                self._lock.writer_leaves() 
    264                  
     261 
    265262    def set(self, key, value, timeout=None): 
    266263        self._lock.writer_enters() 
     
    269266        finally: 
    270267            self._lock.writer_leaves() 
    271              
     268 
    272269    def delete(self, key): 
    273270        self._lock.writer_enters() 
     
    285282 
    286283class _FileCache(_SimpleCache): 
    287     """File-based cache""" 
    288      
     284    "File-based cache." 
    289285    def __init__(self, dir, params): 
    290286        self._dir = dir 
     
    294290        del self._cache 
    295291        del self._expire_info 
    296          
     292 
    297293    def get(self, key, default=None): 
    298294        fname = self._key_to_file(key) 
     
    309305            pass 
    310306        return default 
    311          
     307 
    312308    def set(self, key, value, timeout=None): 
    313309        fname = self._key_to_file(key) 
     
    328324        except (IOError, OSError): 
    329325            raise 
    330              
     326 
    331327    def delete(self, key): 
    332328        try: 
     
    334330        except (IOError, OSError): 
    335331            pass 
    336              
     332 
    337333    def has_key(self, key): 
    338334        return os.path.exists(self._key_to_file(key)) 
    339          
     335 
    340336    def _cull(self, filelist): 
    341337        if self.cull_frequency == 0: 
     
    349345                pass 
    350346 
    351     def _createdir(self):     
     347    def _createdir(self): 
    352348        try: 
    353349            os.makedirs(self._dir) 
     
    367363 
    368364class _DBCache(_Cache): 
    369     """SQL cache backend""" 
    370      
     365    "SQL cache backend." 
    371366    def __init__(self, table, params): 
    372367        _Cache.__init__(self, params) 
    373368        self._table = table 
    374         max_entries = params.get('max_entries', 300)  
    375         try:  
    376             self._max_entries = int(max_entries)  
    377         except (ValueError, TypeError):  
    378             self._max_entries = 300  
    379         cull_frequency = params.get('cull_frequency', 3)  
    380         try:  
    381             self._cull_frequency = int(cull_frequency)  
    382         except (ValueError, TypeError):  
    383             self._cull_frequency = 3  
    384          
     369        max_entries = params.get('max_entries', 300) 
     370        try: 
     371            self._max_entries = int(max_entries) 
     372        except (ValueError, TypeError): 
     373            self._max_entries = 300 
     374        cull_frequency = params.get('cull_frequency', 3) 
     375        try: 
     376            self._cull_frequency = int(cull_frequency) 
     377        except (ValueError, TypeError): 
     378            self._cull_frequency = 3 
     379 
    385380    def get(self, key, default=None): 
    386381        cursor = db.cursor() 
     
    395390            return default 
    396391        return pickle.loads(base64.decodestring(row[1])) 
    397          
     392 
    398393    def set(self, key, value, timeout=None): 
    399394        if timeout is None: 
     
    418413        else: 
    419414            db.commit() 
    420          
     415 
    421416    def delete(self, key): 
    422417        cursor = db.cursor() 
    423418        cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 
    424419        db.commit() 
    425          
     420 
    426421    def has_key(self, key): 
    427422        cursor = db.cursor() 
    428423        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 
    429424        return cursor.fetchone() is not None 
    430          
     425 
    431426    def _cull(self, cursor, now): 
    432427        if self._cull_frequency == 0: 
     
    439434                cursor.execute("SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s" % self._table, [num / self._cull_frequency]) 
    440435                cursor.execute("DELETE FROM %s WHERE cache_key < %%s" % self._table, [cursor.fetchone()[0]]) 
    441          
     436 
    442437########################################## 
    443438# Read settings and load a cache backend # 
  • django/trunk/django/middleware/cache.py

    r810 r821  
    1 import copy 
    21from django.conf import settings 
    32from django.core.cache import cache 
     
    5049 
    5150        request._cache_update_cache = False 
    52         return copy.copy(response) 
     51        return response 
    5352 
    5453    def process_response(self, request, response):