Skip to content

Commit

Permalink
Merge pull request #23 from luizalabs/ro-lock_default_timeout
Browse files Browse the repository at this point in the history
[PROPOSAL] Use cache backend default timeout when expire param is not given.
  • Loading branch information
renanivo committed Feb 9, 2017
2 parents a311f22 + 7b284a2 commit 6eb161d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion django_toolkit/concurrent/locks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.core.cache import caches
from django.core.cache.backends.base import DEFAULT_TIMEOUT


class LockActiveError(Exception):
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(
self,
key,
cache_alias='default',
expire=0,
expire=DEFAULT_TIMEOUT,
raise_exception=True
):
super(CacheLock, self).__init__()
Expand Down
1 change: 1 addition & 0 deletions docs/concurrent.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Django cache alias.
`expire`

Time in seconds to expire the cache.
This argument defaults to the backend timeout configuration.

`raise_exception`

Expand Down
8 changes: 8 additions & 0 deletions tests/concurrent/test_locks.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ def test_should_not_raise_exception_when_raise_exception_is_false(self):

with CacheLock(key='test', raise_exception=False) as lock:
assert lock.active is False

def test_should_use_default_cache_timeout_when_expire_is_not_given(self):
with CacheLock(cache_alias='explicit_timeout', key='test') as lock:
assert lock.cache.get(lock._key)

def test_should_expire_immediately_when_expire_is_zero(self):
with CacheLock(key='test', expire=0) as lock:
assert not lock.cache.get(lock._key)
6 changes: 5 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
},
'access_token': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
}
},
'explicit_timeout': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'TIMEOUT': 300,
},
}

TOOLKIT = {
Expand Down

0 comments on commit 6eb161d

Please sign in to comment.