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.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@699 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jacob Kaplan-Moss 2005-09-26 12:38:41 +00:00
parent c44fd057fa
commit adce221d92
1 changed files with 22 additions and 11 deletions

View File

@ -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 @@ class _FileCache(_SimpleCache):
fname = self._key_to_file(key)
if timeout is None:
timeout = self.default_timeout
try:
filelist = os.listdir(self._dir)
except (IOError, OSError):
self._createdir()
if len(filelist) > self._max_entries:
self._cull(filelist)
try:
@ -347,6 +347,12 @@ class _FileCache(_SimpleCache):
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 @@ class _FileCache(_SimpleCache):
#############
import base64
from django.core.db import db
from django.core.db import db, DatabaseError
from datetime import datetime
class _DBCache(_Cache):
@ -400,10 +406,15 @@ class _DBCache(_Cache):
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])
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:
db.commit()
def delete(self, key):