django-cache-lock
A simple lock extension for django's cache to prevent concurrent editing.
Installation
Install django-cache-lock by using pip
pip install django-cache-lock
Quick Start
You can work with django-cache-lock by using with-statement or decorator.
from django_lock import lock
with lock("global"):
pass
@lock("global")
def foo():
pass
A shortcut to lock model instance
from django.db import models
from django_lock import model_lock
class Foo(models.Model):
bar = models.CharField(max_length=8)
@lock_model
def lock_pk(self):
pass
@lock_model("bar", blocking=False)
def lock_bar(self):
pass
def nolock(self):
pass
foo = Foo()
with lock_model(foo, blocking=False):
nolock()
Configurations
| key | default | desc |
|---|---|---|
| DJANGOLOCK_PREFIX | "lock:" | lock's key prefix stored in cache |
| DJANGOLOCK_SLEEP | 0.1 | default interval time to acquire a lock if a lock is holded by others |
| DJANGOLOCK_RELEASEONDEL | True | release lock when __del__ is called if True |
Advanced usage
For more usages, please read the code.
Supported backends
- django.core.cache.backends.db
- django.core.cache.backends.file
- django.core.cache.backends.locmem
- django.core.cache.backends.memcached
- django-redis
- django-redis-cache
ATTENTIONS
locmem backend
- DO NOT USE locmem backend in a product environment.
memcached backend
- Memcached does not support milliseconds expire time, and its' expire time is not very exact. So memcached lock's timeout time is not as exact as other backends.
redis backend
- We didn't test distributed redis lock.
TODOS:
- use memcached's cas to release lock
- reacquire and extend lock
- database backend cache support
Release files for django-cache-lock 0.2.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-cache-lock-0.2.5.tar.gz | 7.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_cache_lock-0.2.5-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 14.9 kB
Release files / django-cache-lock-0.2.5.tar.gz
| Download URL | django-cache-lock-0.2.5.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
27a688c4b0f547c7066a0da827e6ff21c22805529afdfc3992d1754d81b6d3c6
|
|
BLAKE2b-256 checksum How to use checksums |
0263966ed2b3180e7f5c3401682cd1be615bf8c8c0db21602128dc0eeca22b87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
|
Release files / django_cache_lock-0.2.5-py2.py3-none-any.whl
| Download URL | django_cache_lock-0.2.5-py2.py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
eaa34a908972ef740a9e1a3131668bff6974c44ad61b09c330f165eee441467c
|
|
BLAKE2b-256 checksum How to use checksums |
c376ca915af2539f912e2d398939d59c7eeae1c82bb36d91bddbca61724f5dd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5
|