Skip to main content

A simple look that uses the cache as acquirer

Project description


[![Supported Versions](](
[![Build Status](](
[![Coverage Status](](
[![PyPI version](](

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


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


with cachelock.Lock(
) ...

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



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.

def func(arg_a, arg_b):

That way, if it is locked, the function quits silently. You can also ask `once` to raise `LockError`:

def func(arg_a, arg_b):

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):

def get(self, key, default=None):

def delete(self, key):

cache = DummyCache()


@cachelock.once(key=..., cache=cache)


with cachelock.Lock(key..., cache=cache) ...

Project details

Download files

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

Files for cachelock, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size cachelock-0.0.3-py3-none-any.whl (6.3 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size cachelock-0.0.3.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page