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
[![Build Status](](

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.

### Example
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
return HttpResponse(data)

### 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.

import os

"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://"),
"TIMEOUT": 1200,


### All fields:
* Required.
* Specified as `redis://host:port` with optional `?db=x` to specify the database.
* Optional. Default: 10. If set to None a single connection will be created instead of a pool.
* `LOOP`
* Optional. Default `None`.
* The IO loop to run everything on. If the loop is not provided `asycnio.get_event_loop()` will be called when
required. The clint is setup lazily so this will not happen until the first time you access the cache.
* Optional. Default 300.
* Specify the default cache expire time. Used in `set` type operations.
* Optional. Default `django_asyncio_redis.serializers.pickle.PickleSerializer`
* Determines how the data is encoded to be transmitted to redis.
* Optional. Default `django_asyncio_redis.compressors.identity.IdentityCompressor`
* Determines how the data is compressed before transmitted to redis.
* Optional. Default `{}`
* If using the pickle serializer you can specify the `PICKLE_VERSION` to use as an integer.

### Notes
- locking implementation based off
- serializers/compressors based off of
- Have not tested it with multiple redis servers.
- Contributions welcome

# Release Notes

### v0.4
- General code cleanup and organization

### Serializers/Encoders
- Pickle encoder added
- msgpack encoder added
- Default encoder switched to pickle encoder.
- JSON Encoder moved from `django_asyncio_redis.encoder.JSONEncoder` to `django_asyncio_redis.serializers.json.JSONSerializer`

### Compression Support
- Compression support added
- lzma and zlib compressors added
- defaulting to no compression

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.4.tar.gz (6.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