Skip to content

Commit 5506eee

Browse files
authored
Merge pull request #726 from sauravsharma1998/master
Fix:Expire method fails when using DEFAULT_TIMEOUT
2 parents 1abeaa5 + 8bf4390 commit 5506eee

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

changelog.d/724.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hotfix for timeout=DEFAULT_TIMEOUT in expire and pexpire

django_redis/client/default.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ def expire(
299299
version: Optional[int] = None,
300300
client: Optional[Redis] = None,
301301
) -> bool:
302+
if timeout is DEFAULT_TIMEOUT:
303+
timeout = self._backend.default_timeout # type: ignore
304+
302305
if client is None:
303306
client = self.get_client(write=True)
304307

@@ -315,6 +318,9 @@ def pexpire(
315318
version: Optional[int] = None,
316319
client: Optional[Redis] = None,
317320
) -> bool:
321+
if timeout is DEFAULT_TIMEOUT:
322+
timeout = self._backend.default_timeout # type: ignore
323+
318324
if client is None:
319325
client = self.get_client(write=True)
320326

tests/test_backend.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import pytest
99
from django.core.cache import caches
10+
from django.core.cache.backends.base import DEFAULT_TIMEOUT
1011
from django.test import override_settings
1112
from pytest_django.fixtures import SettingsWrapper
1213
from pytest_mock import MockerFixture
@@ -606,6 +607,11 @@ def test_expire(self, cache: RedisCache):
606607
assert pytest.approx(ttl) == 20
607608
assert cache.expire("not-existent-key", 20) is False
608609

610+
def test_expire_with_default_timeout(self, cache: RedisCache):
611+
cache.set("foo", "bar", timeout=None)
612+
assert cache.expire("foo", DEFAULT_TIMEOUT) is True
613+
assert cache.expire("not-existent-key", DEFAULT_TIMEOUT) is False
614+
609615
def test_pexpire(self, cache: RedisCache):
610616
cache.set("foo", "bar", timeout=None)
611617
assert cache.pexpire("foo", 20500) is True
@@ -614,6 +620,11 @@ def test_pexpire(self, cache: RedisCache):
614620
assert pytest.approx(ttl, 10) == 20500
615621
assert cache.pexpire("not-existent-key", 20500) is False
616622

623+
def test_pexpire_with_default_timeout(self, cache: RedisCache):
624+
cache.set("foo", "bar", timeout=None)
625+
assert cache.pexpire("foo", DEFAULT_TIMEOUT) is True
626+
assert cache.pexpire("not-existent-key", DEFAULT_TIMEOUT) is False
627+
617628
def test_pexpire_at(self, cache: RedisCache):
618629
# Test settings expiration time 1 hour ahead by datetime.
619630
cache.set("foo", "bar", timeout=None)

0 commit comments

Comments
 (0)