#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.