Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#28601 closed Cleanup/optimization (fixed)

Don't cache default callable return value of None in cache.get_or_set()

Reported by: Dan Tao Owned by: nobody
Component: Core (Cache system) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Dan Tao)

In 82be474 the change was made to allow None as a default value passed to
BaseCache.get_or_set. In case the default value of None is used, it is not
stored in the cache. This still left open the possibility that default could
be a callable that returns None, in which case it would be stored in the
cache.

# This scenario works as expected.
cache.get_or_set('foo', None)  # None
cache.get_or_set('foo', 5)  # 5
cache.get('foo')  # 5

# This scenario seems wrong.
cache.get_or_set('bar', lambda: None)  # None
cache.get_or_set('bar', 5)  # None :(
cache.get('bar')  # None :(

Change History (8)

comment:1 Changed 2 years ago by Dan Tao

I have already created a pull request for this issue, but I'm not sure how to officially associate it with this ticket so I'll just link to it here in a comment:

https://github.com/django/django/pull/9087

comment:2 Changed 2 years ago by Sergey Fedoseev

Has patch: set

comment:3 Changed 2 years ago by Claude Paroz

Triage Stage: UnreviewedAccepted
Version: 1.11master

comment:4 Changed 2 years ago by Dan Tao

Description: modified (diff)

comment:5 Changed 2 years ago by Tim Graham

Summary: do not cache default callable return value of None in get_or_setDon't cache default callable return value of None in cache.get_or_set()
Triage Stage: AcceptedReady for checkin
Version: master1.11

comment:6 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In 4d60261b:

Fixed #28601 -- Prevented cache.get_or_set() from caching None if default is a callable that returns None.

comment:7 Changed 2 years ago by Tim Graham <timograham@…>

In dc112bf5:

[2.0.x] Fixed #28601 -- Prevented cache.get_or_set() from caching None if default is a callable that returns None.

Backport of 4d60261b2a77460b4c127c3d832518b95e11a0ac from master

comment:8 Changed 2 years ago by Tim Graham <timograham@…>

In 45b0ec8:

[1.11.x] Fixed #28601 -- Prevented cache.get_or_set() from caching None if default is a callable that returns None.

Backport of 4d60261b2a77460b4c127c3d832518b95e11a0ac from master

Note: See TracTickets for help on using tickets.
Back to Top