[Django] #36610: [Cache] DatabaseCache resets expiration date on incr/decr

1 view
Skip to first unread message

Django

unread,
Sep 15, 2025, 4:15:50 AM9/15/25
to django-...@googlegroups.com
#36610: [Cache] DatabaseCache resets expiration date on incr/decr
-------------------------------------+-------------------------------------
Reporter: simone- | Owner: simone-macri
macri |
Type: Bug | Status: assigned
Component: Core | Version: 5.2
(Cache system) |
Severity: Normal | Keywords: cache incr dect
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
[Cache] DatabaseCache resets expiration date on incr/decr

Problem description

When using DatabaseCache with the incr() and decr() methods, if a key
already has a custom timeout set, its expiration date is reset to the
default value instead of keeping the existing one.

This leads to loss of the correct expiry value, causing keys with custom
timeouts to be invalidated earlier or later than expected.

Steps to reproduce
1. Configure a DatabaseCache.
2. Save a key with an explicit timeout:

cache.set("key", 1, timeout=86400) # 1 day


3. Check the expiration in the DB (correct: ~current_time +
86400).
4. Increment the value with:

cache.incr("key")


5. Check the expiration again: it is reset to the default
instead of remaining unchanged.

Expected behavior

The incr() and decr() operations should not modify the expiration date of
an existing key. They should preserve the previously defined timeout.

Proposed changes
• Introduced a get_many_rows() method to isolate and
directly test expiry handling.
• Added a no_timeout flag in _base_set to distinguish cases
where no new timeout should be set.
• Updated the UPDATE logic so that incr/decr does not
overwrite the expires field.
• Added tests to ensure incr() and decr() maintain the
original expiry



Affected files
• django/core/cache/backends/db.py
• tests/cache/tests.py
--
Ticket URL: <https://code.djangoproject.com/ticket/36610>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 15, 2025, 4:20:11 AM9/15/25
to django-...@googlegroups.com
#36610: [Cache] DatabaseCache resets expiration date on incr/decr
-------------------------------------+-------------------------------------
Reporter: simone-macri | Owner: simone-
| macri
Type: Bug | Status: assigned
Component: Core (Cache system) | Version: 5.2
Severity: Normal | Resolution:
Keywords: cache incr dect | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by simone-macri:

Old description:
New description:
--
--
Ticket URL: <https://code.djangoproject.com/ticket/36610#comment:1>

Django

unread,
Sep 17, 2025, 6:15:47 AM9/17/25
to django-...@googlegroups.com
#36610: [Cache] DatabaseCache resets expiration date on incr/decr
-------------------------------------+-------------------------------------
Reporter: Simone Macri | Owner: Simone
| Macri
Type: Bug | Status: closed
Component: Core (Cache system) | Version: 5.2
Severity: Normal | Resolution: duplicate
Keywords: cache incr dect | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

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

Comment:

Duplicate of #26619
--
Ticket URL: <https://code.djangoproject.com/ticket/36610#comment:2>
Reply all
Reply to author
Forward
0 new messages