Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A simple lock extension for django's cache.

Project description

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

  • 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-cache-lock, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size django_cache_lock-0.2.2-py2.py3-none-any.whl (7.1 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size django-cache-lock-0.2.2.tar.gz (7.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page