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 3 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
3 changes: 3 additions & 0 deletions changelog.d/724.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix for Issue 724(expire method fails when using DEFAULT_TIMEOUT)

https://github.com/jazzband/django-redis/issues/724
Copy link
Member

Choose a reason for hiding this comment

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

no need to point out the issue just write an explanation

7 changes: 7 additions & 0 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ def expire(
version: Optional[int] = None,
client: Optional[Redis] = None,
) -> bool:
# timeout could be of type int|timedelta
# if timeout is DEFAULT_TIMEOUT or None then
# use self._backend.default_timeout(django default cache timeout)
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 please remove this?


if (timeout is DEFAULT_TIMEOUT) or (timeout is None):
sauravsharma1998 marked this conversation as resolved.
Show resolved Hide resolved
timeout = self._backend.default_timeout

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

Expand Down
5 changes: 5 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,10 @@ 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
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?


def test_pexpire(self, cache: RedisCache):
cache.set("foo", "bar", timeout=None)
assert cache.pexpire("foo", 20500) is True
Expand Down
Loading