-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/UnitapApp/unitap-backend …
…into develop
- Loading branch information
Showing
9 changed files
with
2,291 additions
and
557 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,20 @@ | ||
import time | ||
from contextlib import contextmanager | ||
from django.core.cache import cache | ||
|
||
@contextmanager | ||
def memcache_lock(lock_id, oid, lock_expire=60): | ||
timeout_at = time.monotonic() + lock_expire | ||
# cache.add fails if the key already exists | ||
status = cache.add(lock_id, oid, 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
Oops, something went wrong.