[Django] #33060: Add a helper function to make and validate cache keys.

34 views
Skip to first unread message

Django

unread,
Aug 26, 2021, 9:09:42 AM8/26/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core | Version: dev
(Cache system) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Following from
[https://github.com/django/django/pull/14437#discussion_r696351175 this
thread] the following pattern is repeated a lot in the cache backends:

{{{#!python
key = self.make_key(key, version=version)
self.validate_key(key)
}}}

We can define a helper function on the base cache backend that can be used
to avoid repetitiveness and help ensure that we consistently call
`.validate_key()` after `.make_key()`:

{{{#!python
def make_and_validate_key(self, key, version=None):
key = self.make_key(key, version=version)
self.validate_key(key)
return key
}}}

An alternative proposal is to have `.make_key()` learn a `validate` flag,
but we'd probably need to have it as `False` by default for backward
compatibility and we'd may still have issues if users have overridden
`.make_key()`. So it would require documentation changes, release notes,
and a deprecation period.

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

Django

unread,
Aug 26, 2021, 9:11:59 AM8/26/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

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

* has_patch: 0 => 1


Comment:

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

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

Django

unread,
Aug 26, 2021, 9:53:09 AM8/26/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | 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):

* stage: Unreviewed => Accepted


Comment:

Adding `make_and_validate_key()` sounds reasonable.

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

Django

unread,
Sep 3, 2021, 4:27:17 AM9/3/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1
* needs_tests: 0 => 1


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

Django

unread,
Sep 3, 2021, 4:38:47 AM9/3/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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

In [changeset:"68b8eda78864b78d1b8b2529ec119ec440f8458a" 68b8eda7]:
{{{
#!CommitTicketReference repository=""
revision="68b8eda78864b78d1b8b2529ec119ec440f8458a"
Refs #33060 -- Added .make_key() in .touch() for dummy cache backend.

All cache operations should use make_key().
}}}

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

Django

unread,
Sep 3, 2021, 5:56:41 AM9/3/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

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

* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


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

Django

unread,
Sep 7, 2021, 6:39:15 AM9/7/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
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:"ec2f6ea9c6203524933e08d069733cd00a09ede3" ec2f6ea9]:
{{{
#!CommitTicketReference repository=""
revision="ec2f6ea9c6203524933e08d069733cd00a09ede3"
Refs #33060 -- Added memcached test for make_key() making keys invalid.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33060#comment:6>

Django

unread,
Sep 7, 2021, 6:39:15 AM9/7/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: closed

Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
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:"42dfa97e191d8f7ffdc0b5d9502949ef3b8ef356" 42dfa97e]:
{{{
#!CommitTicketReference repository=""
revision="42dfa97e191d8f7ffdc0b5d9502949ef3b8ef356"
Fixed #33060 -- Added BaseCache.make_and_validate_key() hook.

This helper function reduces the amount of duplicated code and makes it
easier to ensure that we always validate the keys.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33060#comment:8>

Django

unread,
Sep 7, 2021, 6:39:16 AM9/7/21
to django-...@googlegroups.com
#33060: Add a helper function to make and validate cache keys.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Core (Cache system) | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
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:"4b82578a6045746e7c470b7881dfcf182fd57048" 4b82578a]:
{{{
#!CommitTicketReference repository=""
revision="4b82578a6045746e7c470b7881dfcf182fd57048"
Refs #33060 -- Ensured cache backends validate keys.

The validate_key() function should be called after make_key() to ensure
that the validation is performed on the key that will actually be
stored in the cache.

Co-authored-by: Mariusz Felisiak <felisiak...@gmail.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33060#comment:7>

Reply all
Reply to author
Forward
0 new messages