A simple lock extension for django's cache.
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-cache-lock-0.2.5.tar.gz
.
File metadata
- Download URL: django-cache-lock-0.2.5.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27a688c4b0f547c7066a0da827e6ff21c22805529afdfc3992d1754d81b6d3c6 |
|
MD5 | b770560a038ec6a341a4a73e75d4828a |
|
BLAKE2b-256 | 0263966ed2b3180e7f5c3401682cd1be615bf8c8c0db21602128dc0eeca22b87 |
File details
Details for the file django_cache_lock-0.2.5-py2.py3-none-any.whl
.
File metadata
- Download URL: django_cache_lock-0.2.5-py2.py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 2, Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eaa34a908972ef740a9e1a3131668bff6974c44ad61b09c330f165eee441467c |
|
MD5 | 4561f83a2db911194924fbee0875bec6 |
|
BLAKE2b-256 | c376ca915af2539f912e2d398939d59c7eeae1c82bb36d91bddbca61724f5dd8 |