Adds redis support to Sanic
Project description
sanic-redis
Async Redis support for Sanic.
Built on top of Async version of Redis library.
HiRedis is available as an optional parser for higher throughput.
Installation
You can install this package as usual with pip:
pip install sanic-redis
Install the optional hiredis parser for higher throughput:
pip install "sanic-redis[hiredis]"
Requires Python 3.10+, Sanic 25.3+, and redis-py 7 or 8.
Config
Redis URLs are passed to redis.asyncio.from_url. See the redis-py URL
documentation for supported schemes and query options:
https://redis.readthedocs.io/en/stable/connections.html#redis.asyncio.client.Redis.from_url
Basic setup:
app.config.REDIS = "redis://localhost:6379/0"
redis = SanicRedis()
redis.init_app(app)
Use ctx_name when the Sanic config key and runtime context name should differ:
app.config.REDIS_CACHE = "redis://localhost:6379/1"
redis = SanicRedis(config_name="REDIS_CACHE", ctx_name="cache")
redis.init_app(app)
Pass redis-py client options with from_url_kwargs:
redis = SanicRedis(from_url_kwargs={"decode_responses": True})
redis.init_app(app)
Use single_connection_client and auto_close_connection_pool as
SanicRedis parameters, not inside from_url_kwargs or Redis URL query
options.
Enable startup validation when the app should fail fast on Redis connection errors:
redis = SanicRedis(ping_on_startup=True)
redis.init_app(app)
Example
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", ctx_name="redis1")
redis1.init_app(app)
redis2 = SanicRedis(config_name="REDIS2", ctx_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):
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. SanicRedis.conn
and SanicRedis.app were removed in 0.7.
Testing
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:
tox -e py313-deps-latest -- -m compat
Resources
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sanic_redis-0.7.0.tar.gz.
File metadata
- Download URL: sanic_redis-0.7.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
727c112f82968cb88d4a6e9290773725782f11349de8edd19e33221bcdef76c5
|
|
| MD5 |
41825ca8558de94501ef1df9bb8f8000
|
|
| BLAKE2b-256 |
494fe80556ae07a27f1465018b978d9b23e90e9f744b6be924180ac9be61da52
|
Provenance
The following attestation bundles were made for sanic_redis-0.7.0.tar.gz:
Publisher:
python-publish.yml on strahe/sanic-redis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sanic_redis-0.7.0.tar.gz -
Subject digest:
727c112f82968cb88d4a6e9290773725782f11349de8edd19e33221bcdef76c5 - Sigstore transparency entry: 2031643954
- Sigstore integration time:
-
Permalink:
strahe/sanic-redis@e93a9eb8121221f1b41c247422db06923d61bd83 -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/strahe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e93a9eb8121221f1b41c247422db06923d61bd83 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sanic_redis-0.7.0-py3-none-any.whl.
File metadata
- Download URL: sanic_redis-0.7.0-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfab2f9317766be14350e06204e86fb73bd9daac290402701ef2d65136f0a553
|
|
| MD5 |
d350d8c5c31a9ce0a9c4092f768d4203
|
|
| BLAKE2b-256 |
9544fdb2e8e102fea6da9096742194fc596fb821d4c3d28e9e65606802c48dfe
|
Provenance
The following attestation bundles were made for sanic_redis-0.7.0-py3-none-any.whl:
Publisher:
python-publish.yml on strahe/sanic-redis
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sanic_redis-0.7.0-py3-none-any.whl -
Subject digest:
dfab2f9317766be14350e06204e86fb73bd9daac290402701ef2d65136f0a553 - Sigstore transparency entry: 2031644066
- Sigstore integration time:
-
Permalink:
strahe/sanic-redis@e93a9eb8121221f1b41c247422db06923d61bd83 -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/strahe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e93a9eb8121221f1b41c247422db06923d61bd83 -
Trigger Event:
release
-
Statement type: