Xavier-Lam / django-cache-lock

A simple lock extension for django's cache.
MIT License
14 stars 9 forks source link
django lock

django-cache-lock

PyPI Build Status Donate with Bitcoin

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

ATTENTIONS

locmem backend

memcached backend

redis backend

TODOS: