Opened 7 years ago

Closed 7 years ago

Last modified 7 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 by Dan Tao, 7 years ago

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 by Sergey Fedoseev, 7 years ago

Has patch: set

comment:3 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedAccepted
Version: 1.11master

comment:4 by Dan Tao, 7 years ago

Description: modified (diff)

comment:5 by Tim Graham, 7 years ago

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 by Tim Graham <timograham@…>, 7 years ago

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 by Tim Graham <timograham@…>, 7 years ago

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 by Tim Graham <timograham@…>, 7 years ago

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