Pessimistic locking for LRU cache. This fixes #8
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent cc95bf2 · Browse files · 1 file·+24 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+24 added−20 removed
- other:+24 added−20 removed
Provenance
- Commit
d4e5438- Tree
c440a7e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
cc95bf2
1 file changed+24 added−20 removed
modified:jinja2/utils.py+24 added-20 removed
428 unmodified lines429 def setdefault(self, key, default=None):430 """Set `default` if the key is not in the cache otherwise431 leave unchanged. Return the value of this key.432 """433+ self._wlock.acquire()434 try:435+ try:434- return self[key]435- except KeyError:436- self[key] = default437- return default436+ return self[key]437+ except KeyError:438+ self[key] = default439+ return default440+ finally:441+ self._wlock.release()442 443 def clear(self):444 """Clear the cache."""445 self._wlock.acquire()22 unmodified lines468 highest priority then.469 470 Raise a `KeyError` if it does not exist.471 """472+ self._wlock.acquire()473+ try:468- rv = self._mapping[key]469- if self._queue[-1] != key:470- try:471- self._remove(key)472- except ValueError:473- # if something removed the key from the container474- # when we read, ignore the ValueError that we would475- # get otherwise.476- pass477- self._append(key)478- return rv474+ rv = self._mapping[key]475+ if self._queue[-1] != key:476+ try:477+ self._remove(key)478+ except ValueError:479+ # if something removed the key from the container480+ # when we read, ignore the ValueError that we would481+ # get otherwise.482+ pass483+ self._append(key)484+ return rv485+ finally:486+ self._wlock.release()487 488 def __setitem__(self, key, value):489 """Sets the value for an item. Moves the item up so that it490 has the highest priority then.491 """492 self._wlock.acquire()493 try:494 if key in self._mapping:487- try:488- self._remove(key)495+ self._remove(key)489- except ValueError:490- # __getitem__ is not locked, it might happen491- pass496 elif len(self._mapping) == self.capacity:497 del self._mapping[self._popleft()]498 self._append(key)499 self._mapping[key] = value126 unmodified lines