Skip to content

Commit 67b47db

Browse files
authored
Merge pull request #752 from WisdomPill/bugfix/596/lock_blocking_params
Lock added blocking parameter
2 parents e11150a + 673064a commit 67b47db

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

changelog.d/752.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added blocking parameter for `cache.lock`

django_redis/client/default.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,7 @@ def expire(
326326

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

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

333331
def pexpire(
334332
self,
@@ -345,11 +343,7 @@ def pexpire(
345343

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

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

354348
def pexpire_at(
355349
self,
@@ -393,6 +387,7 @@ def lock(
393387
version: Optional[int] = None,
394388
timeout: Optional[float] = None,
395389
sleep: float = 0.1,
390+
blocking: bool = True,
396391
blocking_timeout: Optional[float] = None,
397392
client: Optional[Redis] = None,
398393
thread_local: bool = True,
@@ -405,6 +400,7 @@ def lock(
405400
key,
406401
timeout=timeout,
407402
sleep=sleep,
403+
blocking=blocking,
408404
blocking_timeout=blocking_timeout,
409405
thread_local=thread_local,
410406
)

django_redis/client/herd.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ def _is_expired(x, herd_timeout: int) -> bool:
2626
return True
2727
val = x + random.randint(1, herd_timeout)
2828

29-
if val >= herd_timeout:
30-
return True
31-
return False
29+
return val >= herd_timeout
3230

3331

3432
class HerdClient(DefaultClient):

tests/test_backend.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,27 @@ def test_expire_at(self, cache: RedisCache):
677677

678678
def test_lock(self, cache: RedisCache):
679679
lock = cache.lock("foobar")
680-
lock.acquire(blocking=True)
680+
assert lock.acquire(blocking=True)
681+
682+
assert cache.has_key("foobar")
683+
lock.release()
684+
assert not cache.has_key("foobar")
685+
686+
def test_lock_not_blocking(self, cache: RedisCache):
687+
lock = cache.lock("foobar")
688+
assert lock.acquire(blocking=False)
689+
690+
lock2 = cache.lock("foobar")
691+
692+
assert not lock2.acquire(blocking=False)
681693

682694
assert cache.has_key("foobar")
683695
lock.release()
684696
assert not cache.has_key("foobar")
685697

686698
def test_lock_released_by_thread(self, cache: RedisCache):
687699
lock = cache.lock("foobar", thread_local=False)
688-
lock.acquire(blocking=True)
700+
assert lock.acquire(blocking=True)
689701

690702
def release_lock(lock_):
691703
lock_.release()

0 commit comments

Comments
 (0)