API Reference¶
Auto-generated from source using mkdocstrings.
redis_client_kit¶
Redis client infrastructure with optional Pydantic, Prometheus, and Dishka support.
RedisMetricsProtocol
¶
Bases: Protocol
Protocol for Redis metrics collection.
Defines the interface for collecting Redis client metrics. Implementation can use any metrics library (Prometheus, StatsD, etc.).
Example implementation: RedisMetrics from redis_client_kit.metrics
Source code in redis_client_kit/protocols.py
record_command(command, status, duration)
¶
Record a completed Redis command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Redis command name (e.g., "GET", "SET", "HGETALL") |
required |
status
|
str
|
Execution status ("success" or "error") |
required |
duration
|
float
|
Command execution duration in seconds |
required |
Source code in redis_client_kit/protocols.py
record_error(error_type)
¶
Record a Redis connection or execution error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_type
|
str
|
Error type name (e.g., "ConnectionError", "TimeoutError") |
required |
record_pool_stats(pool_size, pool_checked_out)
¶
RedisSettingsProtocol
¶
Bases: Protocol
Protocol for Redis configuration with grouped settings.
Examples:
Single node setup:
>>> class Settings(RedisSettingsProtocol):
... connection = RedisConnectionSettings(host="localhost", port=6379, db=0)
... cluster = RedisClusterSettings(enabled=False)
... # ... other attributes ...
Redis Cluster setup:
>>> class ClusterSettings(RedisSettingsProtocol):
... connection = RedisConnectionSettings(...)
... cluster = RedisClusterSettings(
... enabled=True,
... nodes=["redis-1:6379", "redis-2:6379", "redis-3:6379"]
... )
... # ... other attributes ...
Source code in redis_client_kit/config.py
build_base_redis_kwargs(settings)
¶
Build base Redis client keyword arguments.
Source code in redis_client_kit/utils.py
build_redis_retry(settings)
¶
Build Redis Retry object from settings.
Source code in redis_client_kit/utils.py
check_async_redis_health(client)
async
¶
Check async Redis connection health.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncRedisClient
|
Redis client (single or cluster) to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if Redis is healthy, False otherwise |
Source code in redis_client_kit/aio/lifecycle.py
check_redis_health(client)
¶
Check sync Redis connection health.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
SyncRedisClient
|
Redis client (single or cluster) to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if Redis is healthy, False otherwise |
Source code in redis_client_kit/sync/lifecycle.py
close_async_redis_client(client)
async
¶
Close async Redis client and release pool connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
AsyncRedisClient
|
Redis client (single or cluster) to close |
required |
Note
Uses asyncio.shield to ensure close operation completes even if task is cancelled.
Source code in redis_client_kit/aio/lifecycle.py
close_redis_client(client)
¶
Close sync Redis client and release pool connections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
SyncRedisClient
|
Redis client (single or cluster) to close |
required |
Source code in redis_client_kit/sync/lifecycle.py
create_async_redis_client(settings, metrics=None)
¶
Create async Redis client with connection pool.
Returns redis-py client (Redis or RedisCluster) with optional metrics instrumentation. - If metrics is None: returns plain Redis/RedisCluster (zero overhead) - If metrics provided: returns InstrumentedRedis/InstrumentedRedisCluster
Automatically selects single or cluster mode based on settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
RedisSettingsProtocol
|
Redis configuration |
required |
metrics
|
RedisMetricsProtocol | None
|
Optional Prometheus metrics collector (None = no instrumentation) |
None
|
Returns:
| Type | Description |
|---|---|
AsyncRedisClient
|
Redis, RedisCluster, InstrumentedRedis, or InstrumentedRedisCluster instance |
Source code in redis_client_kit/aio/factory.py
create_redis_client(settings, metrics=None)
¶
Create sync Redis client with connection pool.
Returns redis-py client (Redis or RedisCluster) with optional metrics instrumentation. - If metrics is None: returns plain Redis/RedisCluster (zero overhead) - If metrics provided: returns InstrumentedRedis/InstrumentedRedisCluster
Automatically selects single or cluster mode based on settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings
|
RedisSettingsProtocol
|
Redis configuration |
required |
metrics
|
RedisMetricsProtocol | None
|
Optional Prometheus metrics collector (None = no instrumentation) |
None
|
Returns:
| Type | Description |
|---|---|
SyncRedisClient
|
Redis, RedisCluster, InstrumentedRedis, or InstrumentedRedisCluster instance |
Source code in redis_client_kit/sync/factory.py
parse_redis_url_node(node)
¶
Parse Redis node from host:port, [ipv6]:port or redis:// URL string.