Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jacobian jacobian authored
Showing with 22 additions and 11 deletions.
  1. +22 −11 django/core/cache.py
View
33 django/core/cache.py
@@ -289,10 +289,7 @@ class _FileCache(_SimpleCache):
def __init__(self, dir, params):
self._dir = dir
if not os.path.exists(self._dir):
- try:
- os.makedirs(self._dir)
- except OSError:
- raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir
+ self._createdir()
_SimpleCache.__init__(self, dir, params)
del self._cache
del self._expire_info
@@ -316,7 +313,10 @@ def set(self, key, value, timeout=None):
fname = self._key_to_file(key)
if timeout is None:
timeout = self.default_timeout
- filelist = os.listdir(self._dir)
+ try:
+ filelist = os.listdir(self._dir)
+ except (IOError, OSError):
+ self._createdir()
if len(filelist) > self._max_entries:
self._cull(filelist)
try:
@@ -346,7 +346,13 @@ def _cull(self, filelist):
os.remove(os.path.join(self._dir, fname))
except (IOError, OSError):
pass
-
+
+ def _createdir(self):
+ try:
+ os.makedirs(self._dir)
+ except OSError:
+ raise EnvironmentError, "Cache directory '%s' does not exist and could not be created'" % self._dir
+
def _key_to_file(self, key):
return os.path.join(self._dir, urllib.quote_plus(key))
@@ -355,7 +361,7 @@ def _key_to_file(self, key):
#############
import base64
-from django.core.db import db
+from django.core.db import db, DatabaseError
from datetime import datetime
class _DBCache(_Cache):
@@ -400,11 +406,16 @@ def set(self, key, value, timeout=None):
self._cull(cursor, now)
encoded = base64.encodestring(pickle.dumps(value, 2)).strip()
cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key])
- if cursor.fetchone():
- cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
+ try:
+ if cursor.fetchone():
+ cursor.execute("UPDATE %s SET value = %%s, expires = %%s WHERE cache_key = %%s" % self._table, [encoded, str(exp), key])
+ else:
+ cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
+ except DatabaseError:
+ # To be threadsafe, updates/inserts are allowed to fail silently
+ pass
else:
- cursor.execute("INSERT INTO %s (cache_key, value, expires) VALUES (%%s, %%s, %%s)" % self._table, [key, encoded, str(exp)])
- db.commit()
+ db.commit()
def delete(self, key):
cursor = db.cursor()
Please sign in to comment.
Something went wrong with that request. Please try again.