django-redis-ex
This library is based on django.core.cache.backends.redis.RedisCache modification.
It uses asyncio Redis to create connections and both asynchronous and synchronous methods are supported. Also fixed a connection pooling bug Django RedisCache (#35651).
User guide
Installation
pip install django-redis-ex
Configure as cache backend
CACHES = {
"default": {
"BACKEND": "django_redis_ex.async_cache.AsyncRedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}
Refer to the official documentation for configuration parameter descriptions.
- https://docs.djangoproject.com/en/5.0/topics/cache/#redis
- https://docs.djangoproject.com/en/5.0/topics/cache/#cache-arguments
You can also use the bug-fixed synchronization cache.
CACHES = {
"default": {
"BACKEND": "django_redis_ex.cache.RedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
}
}
Notes
Although RedisEXCache, AsyncRedisEXCache support both asynchronous and synchronous methods, it is recommended to
use RedisEXCache for synchronous projects and AsyncRedisEXCache for asynchronous projects.
If your project contains both synchronous and asynchronous code, it is recommended to add two caches (one synchronous and one asynchronous). For example:
CACHES = {
"default": {
"BACKEND": "django_redis_ex.async_cache.AsyncRedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
},
"sync_cache": {
"BACKEND": "django_redis_ex.cache.RedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
},
}
from django.core.cache import caches
def sync_do():
caches['sync_cache'].get('key')
async def async_do():
await caches['default'].aget('key')
About Session
Since Django's session does not yet support asynchrony, if you are using a cache as a session backend, it is recommended to add a synchronized cache and set it as the session backend.
CACHES = {
"default": {
"BACKEND": "django_redis_ex.async_cache.AsyncRedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
},
"sync_cache": {
"BACKEND": "django_redis_ex.cache.RedisEXCache",
"LOCATION": "redis://127.0.0.1:6379",
},
}
SESSION_CACHE_ALIAS = 'sync_cache'
Raw client
The synchronization method of AsyncRedisEXCache closes the connection after use, and if you need to use the raw client
of AsyncRedisEXCache in a synchronization function, you likewise need to close the connection after use.
from django.core.cache import cache
from asgiref.sync import async_to_sync
async def aget_data():
client = cache._cache.get_client(write=False)
a = await client.get("a")
b = await client.get("b")
await client.aclose(close_connection_pool=True)
return a, b
def get_data():
return async_to_sync(aget_data)()
Metadata
Release files for django-redis-ex 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_redis_ex-1.0.0.tar.gz | 19.5 kB | Without provenance | Details for django_redis_ex-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_redis_ex-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for django_redis_ex-1.0.0-py3-none-any.whl |
Total release size: 37.1 kB
Release files / django_redis_ex-1.0.0.tar.gz
| Download URL | django_redis_ex-1.0.0.tar.gz |
|---|---|
| Size | 19.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a36d49f3fc808aa546fd862792d62a717c41adee5bab8c57dc54ef9a1183743c
|
|
BLAKE2b-256 checksum How to use checksums |
14072a78ea692efb68601a792f739c0d29bea8e8cf4bc10347835c8a33a83f4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Release files / django_redis_ex-1.0.0-py3-none-any.whl
| Download URL | django_redis_ex-1.0.0-py3-none-any.whl |
|---|---|
| Size | 17.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f45dbe0cb5a1c6be2b36f42501d644a8ba37df7a848f7ecaddce95c9b8738b15
|
|
BLAKE2b-256 checksum How to use checksums |
b104166c0cc974c2f4ee748ffd8819228925b98638a1d7bc290d1bcd879c596a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.10.14
|
Security for django-redis-ex 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.