-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .base import Storage | ||
|
||
__all__ = ['Storage'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Storage(object): | ||
''' | ||
Defines the public api for Storages | ||
''' | ||
|
||
default_config = {} | ||
|
||
def __init__(self, connection, **config): | ||
self.connection = connection | ||
self.config = self.default_config.copy() | ||
self.config.update(config) | ||
|
||
def obtain_lock(self, key, value): | ||
''' | ||
Responsible for obtaining a lock for the task | ||
:returns boolean: Whether lock could be obtained or not | ||
''' | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from . import Storage | ||
|
||
class RedisStorage(Storage): | ||
''' | ||
Storage class for redis. | ||
Config: | ||
expiry: number of seconds before expiring task, 0 to disable (default: 0) | ||
''' | ||
|
||
default_config = { | ||
'expiry': 0, | ||
} | ||
|
||
def obtain_lock(self, key, value): | ||
obtained = self.connection.setnx(key, value) | ||
if self.config['expiry'] and obtained: | ||
self.connection.expire(key, self.config['expiry']) | ||
|
||
return obtained | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exam==0.10.5 | ||
pytest==2.7.2 | ||
redis==2.10.3 |
Empty file.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from uuid import uuid4 | ||
from redis import StrictRedis | ||
|
||
from celery_dedupe.storage.redis import RedisStorage | ||
|
||
class TestRedisStorage(object): | ||
|
||
@property | ||
def redis(self): | ||
if hasattr(self, '_redis'): | ||
return self._redis | ||
|
||
self._redis = StrictRedis() | ||
return self._redis | ||
|
||
def test_lock_obtained(self): | ||
storage = RedisStorage(self.redis) | ||
assert storage.obtain_lock(uuid4(), '1') | ||
|
||
def test_lock_obtained_with_expiry(self): | ||
key = uuid4() | ||
storage = RedisStorage(self.redis, expiry=10) | ||
assert storage.obtain_lock(key, '1') | ||
assert self.redis.ttl(key) == 10 | ||
|
||
def test_already_locked(self): | ||
key = uuid4() | ||
self.redis.setex(key, '1', 10) | ||
storage = RedisStorage(self.redis, expiry=10) | ||
assert not storage.obtain_lock(key, '1') |