Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

A asyncio-redis backend for Django!

Project description

# django-asyncio-redis
A `aioredis` backend for Django. I am using it in production but you may not want to: the API is fairly stable but could change in the future.

### Usage
`pip install django-asyncio-redis`

The following will set your cache backend to the `AsyncRedisCache`. It will be using `HiRedis` and connection pooling.
HiRedis is the preferred backend as it is very fast.


```python
# settings.py
import os

CACHES = {
"default": {}, # Leave your default the way it was for non async django access.
"async": {
"BACKEND": "django-asyncio-redis.cache.AsyncRedisCache",
"LOCATION": os.environ.get('REDIS_LOCATION', "redis://127.0.0.1:6379?db=1"),
"POOLSIZE": 5,
"TIMEOUT": 1200,
}
}

```

### All fields:
* `LOCATION`
* Required.
* Specified as `redis://host:port` with optional `?db=x` to specify the database.
* `POOLSIZE`
* Optional. Default: 10.
* `LOOP`
* Optional. Default `None`.
* The IO loop to run everything on.
* `TIMEOUT`
* Optional. Default 300.
* Specify the default cache expire time. Used in `set` type operations.
* `ENCODER`
* Optional. Default `django_asyncio_redis.encoder.JSONEncoder`
* Determines how the data is encoded to be transmitted to redis.

### Example
```python
# views.py
from django.core.cache import caches
from django.http import HttpResponse


acache = caches['async']

async def my_view(request):
val = await acache.get('test-key')
return HttpResponse(val)

async def my_view_with_context_manager(request):
async with await acache.lock('my-lock-key', timeout=0) as lock:
if not lock.was_obtained:
return HttpResponse('Could not obtain lock', status=400)
data = do_locked_processing() # hold lock until completely done
return HttpResponse(data)

async def my_view_with_context_manager_timed(request):
async with await acache.lock('my-lock-key', timeout=10) as lock:
if not lock.was_obtained:
return HttpResponse('Could not obtain lock', status=400)
data = do_locked_processing() # Hold lock for 10 seconds, otherwise it timed out
if await lock.is_locked: # check lock after long processing
do_more_processing(data)
return HttpResponse(data)
```


### Notes
- locking implementation based off https://github.com/joanvila/aioredlock
- Have not tested it with multiple redis servers.
- Contributions welcome

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
django_asyncio_redis-0.3.3-py3-none-any.whl (6.9 kB) Copy SHA256 hash SHA256 Wheel py3
django-asyncio-redis-0.3.3.tar.gz (4.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page