Skip to content

Commit

Permalink
Pessimistic locking for LRU cache. This fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Apr 12, 2013
1 parent cc95bf2 commit d4e5438
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions jinja2/utils.py
Expand Up @@ -430,11 +430,15 @@ def setdefault(self, key, default=None):
"""Set `default` if the key is not in the cache otherwise
leave unchanged. Return the value of this key.
"""
self._wlock.acquire()
try:
return self[key]
except KeyError:
self[key] = default
return default
try:
return self[key]
except KeyError:
self[key] = default
return default
finally:
self._wlock.release()

def clear(self):
"""Clear the cache."""
Expand Down Expand Up @@ -465,17 +469,21 @@ def __getitem__(self, key):
Raise a `KeyError` if it does not exist.
"""
rv = self._mapping[key]
if self._queue[-1] != key:
try:
self._remove(key)
except ValueError:
# if something removed the key from the container
# when we read, ignore the ValueError that we would
# get otherwise.
pass
self._append(key)
return rv
self._wlock.acquire()
try:
rv = self._mapping[key]
if self._queue[-1] != key:
try:
self._remove(key)
except ValueError:
# if something removed the key from the container
# when we read, ignore the ValueError that we would
# get otherwise.
pass
self._append(key)
return rv
finally:
self._wlock.release()

def __setitem__(self, key, value):
"""Sets the value for an item. Moves the item up so that it
Expand All @@ -484,11 +492,7 @@ def __setitem__(self, key, value):
self._wlock.acquire()
try:
if key in self._mapping:
try:
self._remove(key)
except ValueError:
# __getitem__ is not locked, it might happen
pass
self._remove(key)
elif len(self._mapping) == self.capacity:
del self._mapping[self._popleft()]
self._append(key)
Expand Down

0 comments on commit d4e5438

Please sign in to comment.