Skip to content

Commit

Permalink
fix: make settings._editable_caches more robust
Browse files Browse the repository at this point in the history
Fixes #1989
  • Loading branch information
jerivas committed Jul 20, 2021
1 parent 46d2b4d commit 99a0441
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions mezzanine/conf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,6 @@ class Placeholder:
bytes: partial(bytes, encoding="utf8"),
}

def __init__(self):
"""
The ``_editable_caches`` attribute maps Request objects to dicts of
editable settings loaded from the database. We cache settings per-
request to ensure that the database is hit at most once per request,
and that each request sees the same settings for its duration.
"""
self._editable_caches = WeakKeyDictionary()

@property
def _current_request(self):
return current_request() or self.NULL_REQUEST
Expand Down Expand Up @@ -159,6 +150,12 @@ def _get_editable(self, request):
``_editable_caches``, a WeakKeyDictionary that will automatically
discard each entry when no more references to the request exist.
"""
if not hasattr(self, "_editable_caches"):
# The ``_editable_caches`` attribute maps Request objects to dicts of
# editable settings loaded from the database. We cache settings per-request
# to ensure that the database is hit at most once per request, and that each
# request sees the same settings for its duration.
self._editable_caches = WeakKeyDictionary()
try:
editable_settings = self._editable_caches[request]
except KeyError:
Expand Down

0 comments on commit 99a0441

Please sign in to comment.