-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Redis lock implementation and Excel export
- Loading branch information
Showing
10 changed files
with
120 additions
and
49 deletions.
There are no files selected for viewing
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
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,48 @@ | ||
import time | ||
import typing | ||
from contextlib import contextmanager | ||
|
||
from django.conf import settings | ||
from django.core.cache import caches | ||
from django.db import models | ||
from django_redis.client import DefaultClient | ||
|
||
cache: DefaultClient = caches["default"] | ||
|
||
|
||
class RedisLockKey(models.TextChoices): | ||
""" | ||
Register for generating lock keys | ||
""" | ||
|
||
_BASE = "dj-lock" | ||
|
||
OPERATION_LEARNING_SUMMARY = _BASE + "-operation-learning-summary-{0}" | ||
OPERATION_LEARNING_SUMMARY_EXPORT = _BASE + "-operation-learning-summary-export-{0}" | ||
|
||
|
||
@contextmanager | ||
def redis_lock( | ||
key: RedisLockKey, | ||
id: typing.Union[int, str], | ||
lock_expire: int = settings.REDIS_DEFAULT_LOCK_EXPIRE, | ||
): | ||
""" | ||
Locking mechanism using Redis | ||
""" | ||
lock_id = key.format(id) | ||
timeout_at = time.monotonic() + lock_expire - 3 | ||
# cache.add fails if the key already exists | ||
status = cache.add(lock_id, 1, lock_expire) | ||
|
||
try: | ||
yield status | ||
finally: | ||
# memcache delete is very slow, but we have to use it to take | ||
# advantage of using add() for atomic locking | ||
if time.monotonic() < timeout_at and status: | ||
# don't release the lock if we exceeded the timeout | ||
# to lessen the chance of releasing an expired lock | ||
# owned by someone else | ||
# also don't release the lock if we didn't acquire it | ||
cache.delete(lock_id) |
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
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
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
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.