Metadata-Version: 2.4
Name: sanic-redis
Version: 0.6.1
Summary: Adds redis support to Sanic
Author-email: octal <octalgah@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/strahe/sanic-redis
Keywords: sanic,redis,hiredis
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Internet :: WWW/HTTP :: Session
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: sanic>=25.0.0
Requires-Dist: redis<7.0,>=6.0.0
Requires-Dist: hiredis<4.0,>=3.0.0
Provides-Extra: test
Requires-Dist: sanic-testing>=24.6.0; extra == "test"
Requires-Dist: pytest>=7.0.0; extra == "test"
Requires-Dist: coverage; extra == "test"
Requires-Dist: pytest-asyncio; extra == "test"
Requires-Dist: ruff; extra == "test"
Requires-Dist: tox>=4; extra == "test"
Dynamic: license-file

sanic-redis
==============

[![Tests](https://github.com/strahe/sanic-redis/workflows/Tests/badge.svg)](https://github.com/strahe/sanic-redis/actions)

Async Redis support for sanic.

Built on top of Async version of [Redis library](https://redis-py.readthedocs.io/en/stable/examples/asyncio_examples.html).

[HiRedis](https://github.com/redis/hiredis-py) is used by default for parsing the read results for a higher performance.

Installation
------------

You can install this package as usual with pip:

    pip install sanic-redis

Config
-----------
For example:
```
redis://[[username]:[password]]@localhost:6379/0
rediss://[[username]:[password]]@localhost:6379/0
unix://[username@]/path/to/socket.sock?db=0[&password=password]
```

Three URL schemes are supported:
  - `redis://` creates a TCP socket connection. See more at:
    <https://www.iana.org/assignments/uri-schemes/prov/redis>
  - `rediss://` creates a SSL wrapped TCP socket connection. See more at:
    <https://www.iana.org/assignments/uri-schemes/prov/rediss>
  - ``unix://``: creates a Unix Domain Socket connection.

Details:
https://github.com/redis/redis-py/blob/0d0cfe66eaa541dfc078398f37277e5de8d11dc8/redis/client.py#L132-L168

All allow querystring options:
```
{
    "db": int,
    "socket_timeout": float,
    "socket_connect_timeout": float,
    "socket_keepalive": to_bool,
    "retry_on_timeout": to_bool,
    "retry_on_error": list,
    "max_connections": int,
    "health_check_interval": int,
    "ssl_check_hostname": to_bool,
    "timeout": float,
}
```
Details:
https://github.com/redis/redis-py/blob/0d0cfe66eaa541dfc078398f37277e5de8d11dc8/redis/connection.py#L1235-L1246

Example
------------

```python
from sanic import Sanic
from sanic.response import text
from sanic_redis import SanicRedis

app = Sanic(__name__)

app.config.update(
    {
        'REDIS': "redis://localhost:6379/0",
        'REDIS1': "redis://localhost:6379/1",
        'REDIS2': "redis://localhost:6379/2",
    }
)

redis = SanicRedis() # default config_name is "REDIS"
redis.init_app(app)

redis1 = SanicRedis(config_name="REDIS1")
redis1.init_app(app)

redis2 = SanicRedis(config_name="REDIS2")
redis2.init_app(app)


@app.route('/test1')
async def test1(request):
    r = request.app.ctx.redis1
    await r.set("key1", "value1")
    result = await r.get("key1")
    return text(str(result))


@app.route('/test2')
async def test2(request):
    r = request.app.ctx.redis
    await r.set('key2', 'value2')
    result = await r.get('key2')
    return text(str(result))


@app.route('/test3')
async def test3(request):
    # request.app.ctx.{redis_name}, the {redis_name} == config_name.lower()
    r = request.app.ctx.redis2
    await r.set('key3', 'value3')
    result = await r.get('key3')
    return text(str(result))


if __name__ == '__main__':
    app.run(debug=True)

```

Use `request.app.ctx.<name>` as the runtime connection source. If one
`SanicRedis` instance is shared across multiple Sanic apps, `conn` is only a
convenience handle for the most recently active app.

Testing
-------

```bash
pip install -e ".[test]"
docker compose -f docker-compose.test.yml up -d
tox -e py313-deps-latest
docker compose -f docker-compose.test.yml down
```

Run the quick compatibility smoke test with:

```bash
tox -e py313-deps-latest -- -m compat
```

Resources
---------

- [PyPI](https://pypi.python.org/pypi/sanic-redis)
