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 `asyncio-redis` backend for Django.

This project is currently in alpha. Basic functionality is working but I am testing out various ideas for loop
management and haven't fully tested all the endpoints yet. Currently, the implementation will setup a connection
on first use with whatever loop the `asyncio-redis` library pulls (`asyncio.get_event_loop()`). This is not exactly an ideal
situation but I haven't been able to figure out how to manage loops in a global state object like `django.core.cache.cache`.

### 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,
"PROTOCOL_CLASS": "asyncio_redis.HiRedisProtocol",
}
}

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

### All fields:
* `LOCATION`
* Required.
* Specified as `redis://host:port` with optional `?db=x` to specify the database.
* `POOLSIZE`
* Optional. Default: 0.
* Enables connection pooling.
* `AUTO_RECONNECT`
* Optional. Default `True`.
* `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.
* `PROTOCOL_CLASS`
* Optional. Default `asyncio_redis.RedisProtocol`.
* Determines which protocol class to use.
* Choices:
* `asyncio_redis.RedisProtocol`
* `asyncio_redis.HiRedisProtocol`

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.1.3-py3-none-any.whl (6.2 kB) Copy SHA256 hash SHA256 Wheel py3
django-asyncio-redis-0.1.3.tar.gz (3.4 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