Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added exception handlers to take care of the bugs with the file and d…

…b 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.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@699 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit adce221d92c12f7603ede272dbc45fc97579039e 1 parent c44fd05
Jacob Kaplan-Moss authored September 26, 2005

Showing 1 changed file with 22 additions and 11 deletions. Show diff stats Hide diff stats

  1. 33  django/core/cache.py
33  django/core/cache.py
@@ -289,10 +289,7 @@ class _FileCache(_SimpleCache):
289 289
     def __init__(self, dir, params):
290 290
         self._dir = dir
291 291
         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()
296 293
         _SimpleCache.__init__(self, dir, params)
297 294
         del self._cache
298 295
         del self._expire_info
@@ -316,7 +313,10 @@ def set(self, key, value, timeout=None):
316 313
         fname = self._key_to_file(key)
317 314
         if timeout is None:
318 315
             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()
320 320
         if len(filelist) > self._max_entries:
321 321
             self._cull(filelist)
322 322
         try:
@@ -346,7 +346,13 @@ def _cull(self, filelist):
346 346
                 os.remove(os.path.join(self._dir, fname))
347 347
             except (IOError, OSError):
348 348
                 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
+
350 356
     def _key_to_file(self, key):
351 357
         return os.path.join(self._dir, urllib.quote_plus(key))
352 358
 
@@ -355,7 +361,7 @@ def _key_to_file(self, key):
355 361
 #############
356 362
 
357 363
 import base64
358  
-from django.core.db import db
  364
+from django.core.db import db, DatabaseError
359 365
 from datetime import datetime
360 366
 
361 367
 class _DBCache(_Cache):
@@ -400,11 +406,16 @@ def set(self, key, value, timeout=None):
400 406
             self._cull(cursor, now)
401 407
         encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
402 408
         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
405 417
         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()
408 419
         
409 420
     def delete(self, key):
410 421
         cursor = db.cursor()

0 notes on commit adce221

Please sign in to comment.
Something went wrong with that request. Please try again.