Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:Expire method fails when using DEFAULT_TIMEOUT #726

Merged
merged 7 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/724.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hotfix for timeout=DEFAULT_TIMEOUT in expire and pexpire
6 changes: 6 additions & 0 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ def expire(
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> bool:
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
sauravsharma1998 marked this conversation as resolved.
Show resolved Hide resolved
timeout = self._backend.default_timeout # type: ignore

if client is None:
client = self.get_client(write=True)

Expand All @@ -315,6 +318,9 @@ def pexpire(
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> bool:
if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would let people set None as timeout and get the exception from the client so that they notice they made a mistake instead of getting a seemingly random expiry time

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @WisdomPill!
Changes are done. Please approve the workflow.

timeout = self._backend.default_timeout # type: ignore

if client is None:
client = self.get_client(write=True)

Expand Down
11 changes: 11 additions & 0 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -606,6 +607,11 @@
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you try to expire a non existing key as well?

assert cache.expire("not-existent-key", DEFAULT_TIMEOUT) is False

Check warning on line 613 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L610-L613

Added lines #L610 - L613 were not covered by tests

def test_pexpire(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
assert cache.pexpire("foo", 20500) is True
Expand All @@ -614,6 +620,11 @@
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

Check warning on line 626 in tests/test_backend.py

View check run for this annotation

Codecov / codecov/patch

tests/test_backend.py#L623-L626

Added lines #L623 - L626 were not covered by tests

def test_pexpire_at(self, cache: RedisCache):
# Test settings expiration time 1 hour ahead by datetime.
cache.set("foo", "bar", timeout=None)
Expand Down
Loading