Django

Code

Changeset 699

Show
Ignore:
Timestamp:
09/26/05 07:38:41 (3 years ago)
Author:
jacob
Message:

Added exception handlers to take care of the bugs with the file and db backends
(refs #515). Eugene, I'm going to leave #515 open; can you check the bug fixes
in this revision and mark the ticket as closed if you're satisfied? I don't
run Django in a threaded environment, so I'm having issues reproducing your
errors.

Files:

Legend:

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

    r695 r699  
    290290        self._dir = dir 
    291291        if not os.path.exists(self._dir): 
    292             try: 
    293                 os.makedirs(self._dir) 
    294             except OSError: 
    295                 raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir 
     292            self._createdir() 
    296293        _SimpleCache.__init__(self, dir, params) 
    297294        del self._cache 
     
    317314        if timeout is None: 
    318315            timeout = self.default_timeout 
    319         filelist = os.listdir(self._dir) 
     316        try: 
     317            filelist = os.listdir(self._dir) 
     318        except (IOError, OSError): 
     319            self._createdir() 
    320320        if len(filelist) > self._max_entries: 
    321321            self._cull(filelist) 
     
    347347            except (IOError, OSError): 
    348348                pass 
    349        
     349 
     350    def _createdir(self):     
     351        try: 
     352            os.makedirs(self._dir) 
     353        except OSError: 
     354            raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir 
     355 
    350356    def _key_to_file(self, key): 
    351357        return os.path.join(self._dir, urllib.quote_plus(key)) 
     
    356362 
    357363import base64 
    358 from django.core.db import db 
     364from django.core.db import db, DatabaseError 
    359365from datetime import datetime 
    360366 
     
    401407        encoded = base64.encodestring(pickle.dumps(value, 2)).strip() 
    402408        cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 
    403         if cursor.fetchone(): 
    404             cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) 
     409        try: 
     410            if cursor.fetchone(): 
     411                cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key]) 
     412            else: 
     413                cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) 
     414        except DatabaseError: 
     415            # To be threadsafe, updates/inserts are allowed to fail silently 
     416            pass 
    405417        else: 
    406             cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)]) 
    407         db.commit() 
     418            db.commit() 
    408419         
    409420    def delete(self, key):