Skip to main content

Adds redis support to Sanic

Project description

sanic-redis

Tests

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:

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

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):
    async with redis1.conn as r:
        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()
    async with request.app.ctx.redis1 as r:
        await r.set('key3', 'value3')
        result = await r.get('key3')
    return text(str(result))


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

Testing

pip install -e .[test]
docker-compose -f docker-compose.test.yml up -d
pytest tests/
docker-compose -f docker-compose.test.yml down

Resources

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

sanic_redis-0.6.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sanic_redis-0.6.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file sanic_redis-0.6.0.tar.gz.

File metadata

  • Download URL: sanic_redis-0.6.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sanic_redis-0.6.0.tar.gz
Algorithm Hash digest
SHA256 e801be810b6ecc51f22a0a6f5543d3a684ab23ecb3decef0af8464679d47acc7
MD5 11e484d072e9b302c25d81b67f51297a
BLAKE2b-256 5df110f56fbd817574058e09b2453474264afad5a203e85cd189f9d5513e0c2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sanic_redis-0.6.0.tar.gz:

Publisher: python-publish.yml on strahe/sanic-redis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sanic_redis-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: sanic_redis-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for sanic_redis-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8cce06d08e46bd19ebe7f89fb8717cbd5db3818238248bb77692640277f48c8
MD5 8f81fa4dd450500a436c6a1856d04afc
BLAKE2b-256 28c9c06be46ee3390a9eafe0a878da9f4f5dcf14f681ad04a5cc78a996e0d7bc

See more details on using hashes here.

Provenance

The following attestation bundles were made for sanic_redis-0.6.0-py3-none-any.whl:

Publisher: python-publish.yml on strahe/sanic-redis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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