-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Patch default cache when using the cache panel #1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
29a2ad6
to
f4b872b
Compare
Codecov Report
@@ Coverage Diff @@
## master #1437 +/- ##
==========================================
+ Coverage 87.44% 87.83% +0.38%
==========================================
Files 29 29
Lines 1577 1586 +9
Branches 220 220
==========================================
+ Hits 1379 1393 +14
+ Misses 146 142 -4
+ Partials 52 51 -1
Continue to review full report at Codecov.
|
1a521dd
to
f4b872b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't appear to be necessary to import ConnectionProxy
. I was able to get the tests to pass with:
cache.cache = cache.caches[DEFAULT_CACHE_ALIAS]
Thanks for pointing out the simpler solution than I was working on.
I used the |
Ah, that's why I was patching the existing connections in #1427 |
The difference with #1427 is the patched class there is In Django 3.1 and below the default proxy created the connection on access time with With the new logic (https://github.com/django/django/blob/7b3ec6bcc8309d5b2003d355fe6f78af89cfeb52/django/core/cache/__init__.py#L49) the default cache is instantiated on load time and when the patch to This is why the old patch can't instrument the default cache but it works when accessing |
Sounds good, but can we work the inline import out of the code? One option would be to create our own version of |
Can I ask why you want to avoid the import? It's actually used to detect if the new patch is needed: try:
from django.utils.connection import ConnectionProxy
cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)
except ImportError:
pass If I'm open to change it to |
The reason for the suggestion is to get the import statement to the top of the file rather than having it inline. It's not about the actual try/except flow. |
f4b872b
to
6b524a6
Compare
Ok, the import is now at the top. It is a minor change and it's still just patching when needed 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should move ahead with this. I have suggested two small-ish changes so that we do not have to use the broad try ... except NameError
clause.
from django.utils.connection import ConnectionProxy | ||
except ImportError: | ||
# Django 3.1 and below doesn't have a ConnectionProxy | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pass | |
ConnectionProxy = None |
try: | ||
cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) | ||
except NameError: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try: | |
cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) | |
except NameError: | |
pass | |
# Wrap the patched cache inside Django's ConnectionProxy | |
if ConnectionProxy: | |
cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS) |
The test for the cache panel are broken on master (ea65261). The problem is caused for a change in how Django handles the default cache connection on version 3.2. This PR handles the new connection system to avoid this problem.