Skip to content

Commit

Permalink
Merge pull request #752 from WisdomPill/bugfix/596/lock_blocking_params
Browse files Browse the repository at this point in the history
Lock added blocking parameter
  • Loading branch information
WisdomPill authored Oct 20, 2024
2 parents e11150a + 673064a commit 67b47db
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.d/752.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added blocking parameter for `cache.lock`
12 changes: 4 additions & 8 deletions django_redis/client/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,7 @@ def expire(

key = self.make_key(key, version=version)

# for some strange reason mypy complains,
# saying that timeout type is float | timedelta
return client.expire(key, timeout) # type: ignore
return client.expire(key, timeout)

def pexpire(
self,
Expand All @@ -345,11 +343,7 @@ def pexpire(

key = self.make_key(key, version=version)

# Temporary casting until https://github.com/redis/redis-py/issues/1664
# is fixed.
# for some strange reason mypy complains,
# saying that timeout type is float | timedelta
return bool(client.pexpire(key, timeout)) # type: ignore
return bool(client.pexpire(key, timeout))

def pexpire_at(
self,
Expand Down Expand Up @@ -393,6 +387,7 @@ def lock(
version: Optional[int] = None,
timeout: Optional[float] = None,
sleep: float = 0.1,
blocking: bool = True,
blocking_timeout: Optional[float] = None,
client: Optional[Redis] = None,
thread_local: bool = True,
Expand All @@ -405,6 +400,7 @@ def lock(
key,
timeout=timeout,
sleep=sleep,
blocking=blocking,
blocking_timeout=blocking_timeout,
thread_local=thread_local,
)
Expand Down
4 changes: 1 addition & 3 deletions django_redis/client/herd.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def _is_expired(x, herd_timeout: int) -> bool:
return True
val = x + random.randint(1, herd_timeout)

if val >= herd_timeout:
return True
return False
return val >= herd_timeout


class HerdClient(DefaultClient):
Expand Down
16 changes: 14 additions & 2 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,27 @@ def test_expire_at(self, cache: RedisCache):

def test_lock(self, cache: RedisCache):
lock = cache.lock("foobar")
lock.acquire(blocking=True)
assert lock.acquire(blocking=True)

assert cache.has_key("foobar")
lock.release()
assert not cache.has_key("foobar")

def test_lock_not_blocking(self, cache: RedisCache):
lock = cache.lock("foobar")
assert lock.acquire(blocking=False)

lock2 = cache.lock("foobar")

assert not lock2.acquire(blocking=False)

assert cache.has_key("foobar")
lock.release()
assert not cache.has_key("foobar")

def test_lock_released_by_thread(self, cache: RedisCache):
lock = cache.lock("foobar", thread_local=False)
lock.acquire(blocking=True)
assert lock.acquire(blocking=True)

def release_lock(lock_):
lock_.release()
Expand Down

0 comments on commit 67b47db

Please sign in to comment.