Redis

This module provides support for connecting to Redis (or Valkey) servers through the Redis library.

Usage

First install the redis extra:

$ pip install zayt_contrib[redis]

Define the configuration properties:

modules = [
    "zayt_contrib.redis", # (1)
]

[redis.default] # (2)
url = "redis://localhost:6379/0"

[redis.other] # (3)
url = "redis://localhost:6379/1"

Note

  1. Activate the module

  2. Connection will be registered without a name

  3. Connection registered with name “other”

Inject the redis.asyncio.Redis service:

from typing import Annotated
from redis.asyncio import Redis
from zayt.di import service, Inject


@service
class MyService:
    # default service
    redis: Annotated[Redis, Inject]

    # named service
    other_redis: Annotated[Redis, Inject("other")]

Redis connections can also be defined with username and password separated from the url, or even with individual components:

[redis.url_username_password] # (1)
url = "redis://localhost:6379/0"
username = "user"
password = "pass"

[redis.individual_components] # (2)
host = "localhost"
port = 6379
db = 0
username = "user"
password = "pass"

Note

  1. Username and password separated from the redis url

  2. Each component defined individually

Example

application/handler.py
from redis.asyncio import Redis
from zayt.web import get

@get
async def index(request, redis: Redis):
    number = await redis.incr("number")
    await request.respond_json({"number": number})
configuration/settings.py
[redis.default]
url = "redis://localhost:6379/0"

Configuration

Services

The extension will look for the following services:

  • redis.asyncio.ConnectionPool

  • redis.credentials.CredentialProvider

  • redis.driver_info.DriverInfo

  • redis.event.EventDispatcher

Example:

from zayt import di
from redis.asyncio import ConnectionPool


@di.service
def connection_pool() -> ConnectionPool:
    return ConnectionPool(...)


@di.service(name="named_connection")
def connection_pool() -> ConnectionPool:
    return ConnectionPool(...)

Configuration options

[redis.default]
url = ""
host = ""
port = 6379
db = 0
username = ""
password = ""
socket_timeout = 1.0
socket_connect_timeout = 1.0
socket_keepalive = false
socket_keepalive_options = {}
unix_socket_path = ""
encoding = ""
encoding_errors = "strict"  # "ignore", "replace"
decode_responses = false
retry_on_timeout = false
retry_on_error = [
    "path.to.Error"
]
ssl = false
ssl_keyfile = ""
ssl_certfile = ""
ssl_cert_reqs = ""
ssl_ca_certs = ""
ssl_ca_data = ""
ssl_check_hostname = false
max_connections = 1
single_connection_client = false
health_check_interval = 1
client_name = ""
lib_name = ""
lib_version = ""
auto_close_connection_pool = false
protocol = 3

[redis.default.retry]
retries = 3
backoff = {
    type = "constant",
    backoff = 1.0,
    # type = "no_backoff",
    # type = "decorrelated_jitter", # "equal_jitter", "exponential", "exponential_with_jitter", "full_jitter"
    # cap = 0.512,
    # base = 0.008,
}

Note

options values are described in redis.asyncio.client.Redis.