Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xavier-lam/django-cache-lock
A simple lock extension for django's cache.
https://github.com/xavier-lam/django-cache-lock
django lock
Last synced: 27 days ago
JSON representation
A simple lock extension for django's cache.
- Host: GitHub
- URL: https://github.com/xavier-lam/django-cache-lock
- Owner: Xavier-Lam
- License: mit
- Created: 2019-04-22T07:50:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-04-03T07:33:32.000Z (over 2 years ago)
- Last Synced: 2024-08-09T15:47:14.449Z (5 months ago)
- Topics: django, lock
- Language: Python
- Homepage:
- Size: 66.4 KB
- Stars: 14
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# django-cache-lock
[![PyPI](https://img.shields.io/pypi/v/django-cache-lock.svg)](https://pypi.org/project/django-cache-lock)
[![Build Status](https://travis-ci.org/Xavier-Lam/django-cache-lock.svg?branch=master)](https://travis-ci.org/Xavier-Lam/django-cache-lock)
[![Donate with Bitcoin](https://en.cryptobadges.io/badge/micro/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)](https://en.cryptobadges.io/donate/1BdJG31zinrMFWxRt2utGBU2jdpv8xSgju)A simple lock extension for django's cache to prevent concurrent editing.
## Installation
Install django-cache-lock by using pippip 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():
passA shortcut to lock model instance
from django.db import models
from django_lock import model_lockclass 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):
passdef nolock(self):
passfoo = 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](django_lock.py).## Supported backends
* django.core.cache.backends.db
* django.core.cache.backends.file
* django.core.cache.backends.locmem
* django.core.cache.backends.memcached
* [django-redis](https://github.com/niwinz/django-redis)
* [django-redis-cache](https://github.com/sebleier/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