Skip to content

Conversation

@federicobond
Copy link
Contributor

Description

This changes the values of the backend columns for cache calls in the Cache panel from <django.core.cache.backends.dummy.DummyCache object at 0x1063950a0> to default or the corresponding alias. This makes it much easier to identify which call is made to which backend when several cache backends share the same engine.

Before:

Captura de pantalla 2025-10-08 a la(s) 9 48 20

After:

Captura de pantalla 2025-10-08 a la(s) 9 49 26

Checklist:

  • I have added the relevant tests for this change.
  • I have added an item to the Pending section of docs/changes.rst.

@federicobond federicobond changed the title Show cache alias instead of backend repr Show cache alias instead of backend repr in calls table Oct 8, 2025
_monkey_patch_cache(cache)
cache._djdt_panel = self
for alias in caches:
if hasattr(caches._connections, alias):
Copy link
Member

@tim-schilling tim-schilling Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's going to be hard for us to remember that this is effectively the old version of initialized_only=True. Do you have any ideas on making this more maintainable? [Specifically if Django changes its implementation upstream.]

The only idea I have is to use a cache._djdt_panel.record = functools.partial(cache._djdt_panel.record, alias=alias)

Though it makes me wonder if we need the panel on the cache at all. Seems like we could just store a _djdt_record function on the cache, where then the partial gets much cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though it makes me wonder if we need the panel on the cache at all. Seems like we could just store a _djdt_record function on the cache, where then the partial gets much cleaner.

We do not need the panel on the cache in the current code, it's just used a monkeypatch marker.

The only idea I have is to use a cache._djdt_panel.record = functools.partial(cache._djdt_panel.record, alias=alias)

I thought about something similar, but it needed more changes to the panel inner workings so I went with the most straightforward implementation to validate interest first. I could try a similar approach, storing a _djdt_record function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came back to rework this but if I understand correctly there is no way to get the alias for the given cache without initializing a connection to the backend as a side effect. Checking for the caches._connections looks like the only way to work around this when monkeypatching existing connections.

Copy link
Member

@tim-schilling tim-schilling Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright thank you @federicobond. We need to document this somehow in the code. I'm between separating it out as it's own function or just commenting the loop here:

def initialized_caches():
    """
    Return the initialized caches and aliases.

    This does the same as`caches.all(initialized_only=True)`, but keeps
    the alias with each cache instance.
    """
    for alias in caches:
        if hasattr(caches._connections, alias):
            yield caches[alias], alias

So the rest can be

for cache, alias in initialized_caches():
    _monkey_patch_cache(caches[alias], alias)
    cache._djdt_panel = self

@tim-schilling
Copy link
Member

@federicobond I spotted a few more clean-ups, but this is on the edge of being good to go. Thank you for being patient!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants