2 Star 5 Fork 0

Xavier-Lam / django-cache-lock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

django-cache-lock

PyPI Build Status

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

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 NO 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
The MIT License (MIT) Copyright (c) 2014-2019 Xavier-Lam Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

一个django的lock拓展,基于django的内置cache. 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/xavier-lam/django-cache-lock.git
git@gitee.com:xavier-lam/django-cache-lock.git
xavier-lam
django-cache-lock
django-cache-lock
master

搜索帮助