[Django] #32747: CacheHandler logic issue

29 views
Skip to first unread message

Django

unread,
May 13, 2021, 12:20:50 PM5/13/21
to django-...@googlegroups.com
#32747: CacheHandler logic issue
-----------------------------------------------+------------------------
Reporter: Alexander Ebral | Owner: nobody
Type: Bug | Status: new
Component: Core (Cache system) | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
After the commit:
https://github.com/django/django/commit/98e05ccde440cc9b768952cc10bc8285f4924e1f
logic of the method "all" from CacheHandler class was changed.

Before:

{{{
def all(self):
return getattr(self._caches, 'caches', {}).values()
}}}


This method returned connections that were created in !__getitem!__

Now:

{{{
def all(self):
return [self[alias] for alias in self]
}}}


Connections return for all "CACHES" from settings.py (in case of absence
- they are forcibly created in self[alias])

Which version of this method seems to be right?

In my case this unnecessary mass initialization of custom diskcache-
classes leads to io-lags.

Snippet that helped me:


{{{
import django.core.cache


def cache_getitem(self, alias, exists_only=False):
try:
return getattr(self._connections, alias)
except AttributeError:
if alias not in self.settings:
raise self.exception_class(f"The connection '{alias}' doesn't
exist.")
if exists_only:
return
conn = self.create_connection(alias)
setattr(self._connections, alias, conn)
return conn


def cache_all(self):
connections = [self.__getitem__(alias, exists_only=True) for alias in
self]
return [conn for conn in connections if conn is not None]


django.core.cache.CacheHandler.all = cache_all
django.core.cache.CacheHandler.__getitem__ = cache_getitem
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32747>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 14, 2021, 12:31:08 AM5/14/21
to django-...@googlegroups.com
#32747: CacheHandler initialize unused caches.
-------------------------------------+------------------------------------

Reporter: Alexander Ebral | Owner: nobody
Type: Bug | Status: new
Component: Core (Cache system) | Version: 3.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* cc: Florian Apolloner (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Thanks for the report. I agree this can cause a performance regression, we
shouldn't initialize unused caches unnecessarily. As far as I'm aware we
can restore the previous behavior with:
{{{
diff --git a/django/core/cache/__init__.py b/django/core/cache/__init__.py
index 05ef3897d0..c008ed1125 100644
--- a/django/core/cache/__init__.py
+++ b/django/core/cache/__init__.py
@@ -43,6 +43,8 @@ class CacheHandler(BaseConnectionHandler):
) from e
return backend_cls(location, params)

+ def all(self):
+ return [self[alias] for alias in self if
hasattr(self._connections, alias)]

caches = CacheHandler()

}}}

Regression in 98e05ccde440cc9b768952cc10bc8285f4924e1f.

--
Ticket URL: <https://code.djangoproject.com/ticket/32747#comment:1>

Django

unread,
May 14, 2021, 5:40:53 AM5/14/21
to django-...@googlegroups.com
#32747: CacheHandler initialize unused caches.
-------------------------------------+-------------------------------------
Reporter: Alexander Ebral | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned

Component: Core (Cache system) | Version: 3.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Mariusz Felisiak
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32747#comment:2>

Django

unread,
May 14, 2021, 6:00:56 AM5/14/21
to django-...@googlegroups.com
#32747: CacheHandler initialize unused caches.
-------------------------------------+-------------------------------------
Reporter: Alexander Ebral | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Core (Cache system) | Version: 3.2
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/14395 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32747#comment:3>

Django

unread,
May 18, 2021, 2:23:50 PM5/18/21
to django-...@googlegroups.com
#32747: CacheHandler initialize unused caches.
-------------------------------------+-------------------------------------
Reporter: Alexander Ebral | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed

Component: Core (Cache system) | Version: 3.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"958cdf65ae90d26236d1815bbba804729595ec7a" 958cdf65]:
{{{
#!CommitTicketReference repository=""
revision="958cdf65ae90d26236d1815bbba804729595ec7a"
Fixed #32747 -- Prevented initialization of unused caches.

Thanks Alexander Ebral for the report.

Regression in 98e05ccde440cc9b768952cc10bc8285f4924e1f.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32747#comment:4>

Django

unread,
May 18, 2021, 2:24:25 PM5/18/21
to django-...@googlegroups.com
#32747: CacheHandler initialize unused caches.
-------------------------------------+-------------------------------------
Reporter: Alexander Ebral | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed
Component: Core (Cache system) | Version: 3.2
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"41e2aa7eb24e564f6984e6e6660b8e5ff5edbd9d" 41e2aa7]:
{{{
#!CommitTicketReference repository=""
revision="41e2aa7eb24e564f6984e6e6660b8e5ff5edbd9d"
[3.2.x] Fixed #32747 -- Prevented initialization of unused caches.

Thanks Alexander Ebral for the report.

Regression in 98e05ccde440cc9b768952cc10bc8285f4924e1f.

Backport of 958cdf65ae90d26236d1815bbba804729595ec7a from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32747#comment:5>

Reply all
Reply to author
Forward
0 new messages