Skip to main content

Async and sync Redis cache backend for Django.

Project description

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.

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

Project details


Download files

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

Source Distribution

django_redis_ex-1.0.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

django_redis_ex-1.0.0-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file django_redis_ex-1.0.0.tar.gz.

File metadata

  • Download URL: django_redis_ex-1.0.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for django_redis_ex-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a36d49f3fc808aa546fd862792d62a717c41adee5bab8c57dc54ef9a1183743c
MD5 d30455ad23067c48821ebec1b683a9e8
BLAKE2b-256 14072a78ea692efb68601a792f739c0d29bea8e8cf4bc10347835c8a33a83f4f

See more details on using hashes here.

File details

Details for the file django_redis_ex-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_redis_ex-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f45dbe0cb5a1c6be2b36f42501d644a8ba37df7a848f7ecaddce95c9b8738b15
MD5 c9037f3606e990a468d9d040554ad15a
BLAKE2b-256 b104166c0cc974c2f4ee748ffd8819228925b98638a1d7bc290d1bcd879c596a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page