Skip to content

Commit 9dc3e30

Browse files
committed
Lazy access to Django settings.
1 parent 65a4349 commit 9dc3e30

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

django_redis/cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
from .exceptions import ConnectionInterrupted
1111

12-
DJANGO_REDIS_SCAN_ITERSIZE = getattr(settings, "DJANGO_REDIS_SCAN_ITERSIZE", 10)
13-
1412
CONNECTION_INTERRUPTED = object()
1513

1614

@@ -105,7 +103,8 @@ def delete(self, *args, **kwargs):
105103

106104
@omit_exception
107105
def delete_pattern(self, *args, **kwargs):
108-
kwargs["itersize"] = kwargs.get("itersize", DJANGO_REDIS_SCAN_ITERSIZE)
106+
if not kwargs.get("itersize"):
107+
kwargs["itersize"] = getattr(settings, "DJANGO_REDIS_SCAN_ITERSIZE", 10)
109108
return self.client.delete_pattern(*args, **kwargs)
110109

111110
@omit_exception

django_redis/client/herd.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@ class Marker:
2121
pass
2222

2323

24-
CACHE_HERD_TIMEOUT = getattr(settings, "CACHE_HERD_TIMEOUT", 60)
25-
26-
27-
def _is_expired(x):
28-
if x >= CACHE_HERD_TIMEOUT:
24+
def _is_expired(x, herd_timeout):
25+
if x >= herd_timeout:
2926
return True
30-
val = x + random.randint(1, CACHE_HERD_TIMEOUT)
27+
val = x + random.randint(1, herd_timeout)
3128

32-
if val >= CACHE_HERD_TIMEOUT:
29+
if val >= herd_timeout:
3330
return True
3431
return False
3532

3633

3734
class HerdClient(DefaultClient):
3835
def __init__(self, *args, **kwargs):
3936
self._marker = Marker()
37+
self._herd_timeout = getattr(settings, "CACHE_HERD_TIMEOUT", 60)
4038
super().__init__(*args, **kwargs)
4139

4240
def _pack(self, value, timeout):
@@ -55,7 +53,7 @@ def _unpack(self, value):
5553
now = int(time.time())
5654
if herd_timeout < now:
5755
x = now - herd_timeout
58-
return unpacked, _is_expired(x)
56+
return unpacked, _is_expired(x, self._herd_timeout)
5957

6058
return unpacked, False
6159

@@ -84,7 +82,7 @@ def set(
8482
)
8583

8684
packed = self._pack(value, timeout)
87-
real_timeout = timeout + CACHE_HERD_TIMEOUT
85+
real_timeout = timeout + self._herd_timeout
8886

8987
return super().set(
9088
key, packed, timeout=real_timeout, version=version, client=client, nx=nx

0 commit comments

Comments
 (0)