django-redis-ex


Namedjango-redis-ex JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummaryAsync and sync Redis cache backend for Django.
upload_time2024-08-02 05:49:16
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://code.djangoproject.com/ticket/35651)).

# User guide

## Installation

```shell
pip install django-redis-ex
```

## Configure as cache backend

```python
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.

```python
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:

```python

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",
    },
}
```

```python
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.

```python
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.

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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-redis-ex",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "gojuukaze <ikaze_email@163.com>",
    "download_url": "https://files.pythonhosted.org/packages/14/07/2a78ea692efb68601a792f739c0d29bea8e8cf4bc10347835c8a33a83f4f/django_redis_ex-1.0.0.tar.gz",
    "platform": null,
    "description": "# django-redis-ex\n\nThis library is based on `django.core.cache.backends.redis.RedisCache` modification.\n\nIt uses asyncio Redis to create connections and both asynchronous and synchronous methods are supported.\nAlso fixed a connection pooling bug Django RedisCache ([#35651](https://code.djangoproject.com/ticket/35651)).\n\n# User guide\n\n## Installation\n\n```shell\npip install django-redis-ex\n```\n\n## Configure as cache backend\n\n```python\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis_ex.async_cache.AsyncRedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    }\n}\n```\n\nRefer to the official documentation for configuration parameter descriptions.\n\n* https://docs.djangoproject.com/en/5.0/topics/cache/#redis\n* https://docs.djangoproject.com/en/5.0/topics/cache/#cache-arguments\n\nYou can also use the bug-fixed synchronization cache.\n\n```python\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis_ex.cache.RedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    }\n}\n```\n\n## Notes\n\nAlthough `RedisEXCache`, `AsyncRedisEXCache` support both asynchronous and synchronous methods, it is recommended to\nuse `RedisEXCache` for synchronous projects and `AsyncRedisEXCache` for asynchronous projects.\n\nIf your project contains both synchronous and asynchronous code, it is recommended to add two caches (one synchronous\nand one asynchronous).\nFor example:\n\n```python\n\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis_ex.async_cache.AsyncRedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    },\n    \"sync_cache\": {\n        \"BACKEND\": \"django_redis_ex.cache.RedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    },\n}\n```\n\n```python\nfrom django.core.cache import caches\n\n\ndef sync_do():\n    caches['sync_cache'].get('key')\n\n\nasync def async_do():\n    await caches['default'].aget('key')\n```\n\n## About Session\n\nSince Django's session does not yet support asynchrony, if you are using a cache as a session backend, it is recommended\nto add a synchronized cache and set it as the session backend.\n\n```python\nCACHES = {\n    \"default\": {\n        \"BACKEND\": \"django_redis_ex.async_cache.AsyncRedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    },\n    \"sync_cache\": {\n        \"BACKEND\": \"django_redis_ex.cache.RedisEXCache\",\n        \"LOCATION\": \"redis://127.0.0.1:6379\",\n    },\n}\n\nSESSION_CACHE_ALIAS = 'sync_cache'\n```\n\n# Raw client\n\nThe synchronization method of `AsyncRedisEXCache` closes the connection after use, and if you need to use the raw client\nof `AsyncRedisEXCache` in a synchronization function, you likewise need to close the connection after use.\n\n```python\nfrom django.core.cache import cache\nfrom asgiref.sync import async_to_sync\n\n\nasync def aget_data():\n    client = cache._cache.get_client(write=False)\n    a = await client.get(\"a\")\n    b = await client.get(\"b\")\n    await client.aclose(close_connection_pool=True)\n    return a, b\n\n\ndef get_data():\n    return async_to_sync(aget_data)()\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Async and sync Redis cache backend for Django.",
    "version": "1.0.0",
    "project_urls": {
        "Homepage": "https://github.com/gojuukaze/django-redis-ex",
        "Issues": "https://github.com/gojuukaze/django-redis-ex/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b104166c0cc974c2f4ee748ffd8819228925b98638a1d7bc290d1bcd879c596a",
                "md5": "c9037f3606e990a468d9d040554ad15a",
                "sha256": "f45dbe0cb5a1c6be2b36f42501d644a8ba37df7a848f7ecaddce95c9b8738b15"
            },
            "downloads": -1,
            "filename": "django_redis_ex-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c9037f3606e990a468d9d040554ad15a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 17597,
            "upload_time": "2024-08-02T05:49:14",
            "upload_time_iso_8601": "2024-08-02T05:49:14.540228Z",
            "url": "https://files.pythonhosted.org/packages/b1/04/166c0cc974c2f4ee748ffd8819228925b98638a1d7bc290d1bcd879c596a/django_redis_ex-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "14072a78ea692efb68601a792f739c0d29bea8e8cf4bc10347835c8a33a83f4f",
                "md5": "d30455ad23067c48821ebec1b683a9e8",
                "sha256": "a36d49f3fc808aa546fd862792d62a717c41adee5bab8c57dc54ef9a1183743c"
            },
            "downloads": -1,
            "filename": "django_redis_ex-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "d30455ad23067c48821ebec1b683a9e8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 19478,
            "upload_time": "2024-08-02T05:49:16",
            "upload_time_iso_8601": "2024-08-02T05:49:16.206732Z",
            "url": "https://files.pythonhosted.org/packages/14/07/2a78ea692efb68601a792f739c0d29bea8e8cf4bc10347835c8a33a83f4f/django_redis_ex-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-02 05:49:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gojuukaze",
    "github_project": "django-redis-ex",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "django-redis-ex"
}
        
Elapsed time: 0.59849s