A simple look that uses the cache as acquirer
Project description
python-cachelock
================
[![Supported Versions](https://img.shields.io/pypi/pyversions/cachelock.svg)](https://pypi.python.org/pypi/cachelock)
[![Build Status](https://travis-ci.org/douglasfarinelli/python-cachelock.svg?branch=master)](https://travis-ci.org/douglasfarinelli/python-cachelock)
[![Coverage Status](https://coveralls.io/repos/github/douglasfarinelli/python-cachelock/badge.svg?branch=master)](https://coveralls.io/github/douglasfarinelli/python-cachelock?branch=master)
[![PyPI version](https://badge.fury.io/py/cachelock.svg)](https://pypi.python.org/pypi/cachelock)
The `cachelock` serves to ensure that your code block or function is executed one at a time using the cache as acquirer.
How to install:
===============
pip install cachelock
or
pipenv install cachelock
How to use
==========
You can use of two forms:
- Use the `Lock` class to your blocks;
- Or use the decorator `once` to block your functions or tasks;
With cachelock.Lock
===================
Arguments:
with cachelock.Lock(
key=...
cache=...,
) ...
You should use the Lock class as with context:
with cachelock.Lock(key='foo'):
...your code ...
If it is already locked, the LockError will be raised.
With cachelock.once
===================
Arguments:
@cachelock.once(
key=...
cache=...,
raises_if_lock=False
)
...
With the decorator, you can guarantee the unique execution of some function. It also allows you to configure your key according to the arguments of the function.
@celery.task
@cachelock.once(key='foo-{arg_a}-{arg_b}')
def func(arg_a, arg_b):
pass
That way, if it is locked, the function quits silently. You can also ask `once` to raise `LockError`:
@celery.task
@cachelock.once(
key='foo-{arg_a}-{arg_b}',
raise_if_lock=True
)
def func(arg_a, arg_b):
pass
Integrate with Django
=====================
The cachelock checks whether django is installed, and if so, it uses django's own cache as aquirer. And if you wish, you can modify the default cache with `DEFAULT_CACHELOCK_ALIAS` configuration through django settings. The value must be an alias of some existing cache, by default it uses `default`
*Remembering that by `default`, `cachelock` uses its own internal cache in memory if there is no integration with `django`.*
Customizing your own cache
==========================
To work it is necessary that the implementation of the cache has the `get`, `delete` and `set` methods. Ex.:
class DummyCache:
def set(self, key, value):
pass
def get(self, key, default=None):
pass
def delete(self, key):
pass
cache = DummyCache()
use:
@cachelock.once(key=..., cache=cache)
or
with cachelock.Lock(key..., cache=cache) ...
================
[![Supported Versions](https://img.shields.io/pypi/pyversions/cachelock.svg)](https://pypi.python.org/pypi/cachelock)
[![Build Status](https://travis-ci.org/douglasfarinelli/python-cachelock.svg?branch=master)](https://travis-ci.org/douglasfarinelli/python-cachelock)
[![Coverage Status](https://coveralls.io/repos/github/douglasfarinelli/python-cachelock/badge.svg?branch=master)](https://coveralls.io/github/douglasfarinelli/python-cachelock?branch=master)
[![PyPI version](https://badge.fury.io/py/cachelock.svg)](https://pypi.python.org/pypi/cachelock)
The `cachelock` serves to ensure that your code block or function is executed one at a time using the cache as acquirer.
How to install:
===============
pip install cachelock
or
pipenv install cachelock
How to use
==========
You can use of two forms:
- Use the `Lock` class to your blocks;
- Or use the decorator `once` to block your functions or tasks;
With cachelock.Lock
===================
Arguments:
with cachelock.Lock(
key=...
cache=...,
) ...
You should use the Lock class as with context:
with cachelock.Lock(key='foo'):
...your code ...
If it is already locked, the LockError will be raised.
With cachelock.once
===================
Arguments:
@cachelock.once(
key=...
cache=...,
raises_if_lock=False
)
...
With the decorator, you can guarantee the unique execution of some function. It also allows you to configure your key according to the arguments of the function.
@celery.task
@cachelock.once(key='foo-{arg_a}-{arg_b}')
def func(arg_a, arg_b):
pass
That way, if it is locked, the function quits silently. You can also ask `once` to raise `LockError`:
@celery.task
@cachelock.once(
key='foo-{arg_a}-{arg_b}',
raise_if_lock=True
)
def func(arg_a, arg_b):
pass
Integrate with Django
=====================
The cachelock checks whether django is installed, and if so, it uses django's own cache as aquirer. And if you wish, you can modify the default cache with `DEFAULT_CACHELOCK_ALIAS` configuration through django settings. The value must be an alias of some existing cache, by default it uses `default`
*Remembering that by `default`, `cachelock` uses its own internal cache in memory if there is no integration with `django`.*
Customizing your own cache
==========================
To work it is necessary that the implementation of the cache has the `get`, `delete` and `set` methods. Ex.:
class DummyCache:
def set(self, key, value):
pass
def get(self, key, default=None):
pass
def delete(self, key):
pass
cache = DummyCache()
use:
@cachelock.once(key=..., cache=cache)
or
with cachelock.Lock(key..., cache=cache) ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cachelock-0.0.3.tar.gz
(4.3 kB
view details)
Built Distribution
File details
Details for the file cachelock-0.0.3.tar.gz
.
File metadata
- Download URL: cachelock-0.0.3.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b039fd0e434f80de241482652a64cc75e177191636f5c386e71c77cc4bff5d33 |
|
MD5 | 1044342496f9bd651f7f387444b62f34 |
|
BLAKE2b-256 | c5aab98cf2438195694507cfe279efe760793c1b410dc4111ef163b4ec4a1f6b |
File details
Details for the file cachelock-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: cachelock-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bad57a8e85019787a42d8eda2937d485279de583b6cb279fd5d529bc51ee993b |
|
MD5 | 23229f9fd7ba9e7d8a50cd10e724969c |
|
BLAKE2b-256 | 7f6c0017f287658e4b512badc352fd0509c112e6407f90d8c5333c40d306629d |