Fixe #6131 -- Fixed a problem with expired keys in the locmem cache. Based on a patch from sherbang.

In passing, changed the get() method to use a similar style to has_key() and made add() fractionally faster with the same sort of change (only compute time.time() when we really need it).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6904 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-12-11 01:20:25 +00:00
parent 90093e7965
commit f425cb8caa
1 changed files with 31 additions and 28 deletions

View File

@ -29,44 +29,31 @@ class CacheClass(BaseCache):
self._lock = RWLock()
def _add(self, key, value, timeout=None):
if len(self._cache) >= self._max_entries:
self._cull()
if timeout is None:
timeout = self.default_timeout
if key not in self._cache.keys():
self._cache[key] = value
self._expire_info[key] = time.time() + timeout
def add(self, key, value, timeout=None):
self._lock.writer_enters()
# Python 2.3 and 2.4 don't allow combined try-except-finally blocks.
try:
exp = self._expire_info.get(key)
if exp is None or exp <= time.time():
try:
self._add(key, pickle.dumps(value), timeout)
self._set(key, pickle.dumps(value), timeout)
except pickle.PickleError:
pass
finally:
self._lock.writer_leaves()
def get(self, key, default=None):
should_delete = False
self._lock.reader_enters()
try:
now = time.time()
exp = self._expire_info.get(key)
if exp is None:
return default
elif exp < now:
should_delete = True
else:
elif exp > time.time():
try:
return pickle.loads(self._cache[key])
except pickle.PickleError:
return default
finally:
self._lock.reader_leaves()
if should_delete:
self._lock.writer_enters()
try:
del self._cache[key]
@ -95,7 +82,23 @@ class CacheClass(BaseCache):
self._lock.writer_leaves()
def has_key(self, key):
return key in self._cache
self._lock.reader_enters()
try:
exp = self._expire_info.get(key)
if exp is None:
return False
elif exp > time.time():
return True
finally:
self._lock.reader_leaves()
self._lock.writer_enters()
try:
del self._cache[key]
del self._expire_info[key]
return False
finally:
self._lock.writer_leaves()
def _cull(self):
if self._cull_frequency == 0: