diff --git a/changelog.d/724.bugfix b/changelog.d/724.bugfix new file mode 100644 index 00000000..36ff0f14 --- /dev/null +++ b/changelog.d/724.bugfix @@ -0,0 +1 @@ +Hotfix for timeout=DEFAULT_TIMEOUT in expire and pexpire \ No newline at end of file diff --git a/django_redis/client/default.py b/django_redis/client/default.py index 7850d3c7..b9a5c1b0 100644 --- a/django_redis/client/default.py +++ b/django_redis/client/default.py @@ -299,6 +299,9 @@ def expire( version: Optional[int] = None, client: Optional[Redis] = None, ) -> bool: + if timeout is DEFAULT_TIMEOUT: + timeout = self._backend.default_timeout # type: ignore + if client is None: client = self.get_client(write=True) @@ -315,6 +318,9 @@ def pexpire( version: Optional[int] = None, client: Optional[Redis] = None, ) -> bool: + if timeout is DEFAULT_TIMEOUT: + timeout = self._backend.default_timeout # type: ignore + if client is None: client = self.get_client(write=True) diff --git a/tests/test_backend.py b/tests/test_backend.py index 550ce79c..4ff60983 100644 --- a/tests/test_backend.py +++ b/tests/test_backend.py @@ -7,6 +7,7 @@ import pytest from django.core.cache import caches +from django.core.cache.backends.base import DEFAULT_TIMEOUT from django.test import override_settings from pytest_django.fixtures import SettingsWrapper from pytest_mock import MockerFixture @@ -606,6 +607,11 @@ def test_expire(self, cache: RedisCache): assert pytest.approx(ttl) == 20 assert cache.expire("not-existent-key", 20) is False + def test_expire_with_default_timeout(self, cache: RedisCache): + cache.set("foo", "bar", timeout=None) + assert cache.expire("foo", DEFAULT_TIMEOUT) is True + assert cache.expire("not-existent-key", DEFAULT_TIMEOUT) is False + def test_pexpire(self, cache: RedisCache): cache.set("foo", "bar", timeout=None) assert cache.pexpire("foo", 20500) is True @@ -614,6 +620,11 @@ def test_pexpire(self, cache: RedisCache): assert pytest.approx(ttl, 10) == 20500 assert cache.pexpire("not-existent-key", 20500) is False + def test_pexpire_with_default_timeout(self, cache: RedisCache): + cache.set("foo", "bar", timeout=None) + assert cache.pexpire("foo", DEFAULT_TIMEOUT) is True + assert cache.pexpire("not-existent-key", DEFAULT_TIMEOUT) is False + def test_pexpire_at(self, cache: RedisCache): # Test settings expiration time 1 hour ahead by datetime. cache.set("foo", "bar", timeout=None)