Skip to content

Using CacheStore with DatabaseCache backend shows BEGIN sql queries #2338

@tim-schilling

Description

@tim-schilling

With the following settings:

CACHES = {
    "default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"},
    "debug-toolbar": {
        "BACKEND": "django.core.cache.backends.db.DatabaseCache",
        "LOCATION": "my_cache_table",
    },
}
DEBUG_TOOLBAR_CONFIG = {
    "TOOLBAR_STORE_CLASS": "debug_toolbar.store.CacheStore",
    "CACHE_BACKEND": "debug-toolbar",
}

Using the example app in the project, you'll find there are BEGIN queries being logged in the SQLPanel.

The stacktrace of these queries are:

/venv/lib/python3.12/site-packages/django/contrib/staticfiles/handlers.py in __call__(80)
  return self.application(environ, start_response)
/venv/lib/python3.12/site-packages/whitenoise/middleware.py in __call__(123)
  return self.get_response(request)
/venv/lib/python3.12/site-packages/django/core/cache/backends/db.py in set(103)
  self._base_set("set", key, value, timeout)
/venv/lib/python3.12/site-packages/django/core/cache/backends/db.py in _base_set(142)
  with transaction.atomic(using=db):

I confirmed by printing the keys to find:

:1:djdt:request_ids
:1:djdt:request_ids
:1:djdt:req:01d681d3894d4aad8110e4b754a7e4cc
:1:djdt:request_ids
:1:djdt:req:01d681d3894d4aad8110e4b754a7e4cc
...

I think this is because tracking.DDT_MODELS.add(cache_table) is conditioned to look at queries hitting the model's table. The begin query doesn't have a table, so it shows up.

I think this means we need to further investigate the approach of having a global ContextVar that controls whether we're tracking data or not. This was brought up before, but I didn't push for it super hard. With a flag, we can just ignore all queries while the toolbar is running. This eliminates the need to be aware of what models are being used.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions