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 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/redisrediss://creates a SSL wrapped TCP socket connection. See more at: https://www.iana.org/assignments/uri-schemes/prov/redissunix://: creates a Unix Domain Socket connection.
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,
}
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")
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
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.6.1.tar.gz.
File metadata
- Download URL: sanic_redis-0.6.1.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
681815e777e10a61ab07813a484e1301c38946253aa9b4d4fb449da5ecff18ff
|
|
| MD5 |
2f2600408b4e8b4b6a0d7ca6d2c01256
|
|
| BLAKE2b-256 |
f1660c2707568716be0a7359267d98d7363ceec18ce5a59d5914c9557647ca0f
|
Provenance
The following attestation bundles were made for sanic_redis-0.6.1.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.6.1.tar.gz -
Subject digest:
681815e777e10a61ab07813a484e1301c38946253aa9b4d4fb449da5ecff18ff - Sigstore transparency entry: 1995862436
- Sigstore integration time:
-
Permalink:
strahe/sanic-redis@e4e3f4441aa7b3b4e2405cb8adecf1cf7fb27aeb -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/strahe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e4e3f4441aa7b3b4e2405cb8adecf1cf7fb27aeb -
Trigger Event:
release
-
Statement type:
File details
Details for the file sanic_redis-0.6.1-py3-none-any.whl.
File metadata
- Download URL: sanic_redis-0.6.1-py3-none-any.whl
- Upload date:
- Size: 6.1 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 |
184a1803df04aff683a8d63cab8aaf4e3250bf3718f8aebf895c765c5bc3fbc7
|
|
| MD5 |
6757af6d9e68d2d59468bace78379cda
|
|
| BLAKE2b-256 |
5ccef4a54c480abe0b03e9f4112cfa9b93e2894d382d19f4e60f0aab028f389c
|
Provenance
The following attestation bundles were made for sanic_redis-0.6.1-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.6.1-py3-none-any.whl -
Subject digest:
184a1803df04aff683a8d63cab8aaf4e3250bf3718f8aebf895c765c5bc3fbc7 - Sigstore transparency entry: 1995862490
- Sigstore integration time:
-
Permalink:
strahe/sanic-redis@e4e3f4441aa7b3b4e2405cb8adecf1cf7fb27aeb -
Branch / Tag:
refs/tags/v0.6.1 - Owner: https://github.com/strahe
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@e4e3f4441aa7b3b4e2405cb8adecf1cf7fb27aeb -
Trigger Event:
release
-
Statement type: