Skip to main content

Redis client library for kiarina namespace

Project description

kiarina-lib-redis

A Python client library for Redis with configuration management and connection pooling.

Purpose

This library provides a thin wrapper around the Redis Python client with:

  • Configuration management using pydantic-settings-manager
  • Automatic connection caching and pooling
  • Built-in retry mechanism for connection failures
  • Support for both synchronous and asynchronous operations

Installation

pip install kiarina-lib-redis

Quick Start

Synchronous Usage

from kiarina.lib.redis import get_redis

# Get a Redis client
redis = get_redis(decode_responses=True)

# Basic operations
redis.set("key", "value")
value = redis.get("key")
print(value)  # 'value'

Asynchronous Usage

from kiarina.lib.redis.asyncio import get_redis

async def main():
    redis = get_redis(decode_responses=True)

    await redis.set("key", "value")
    value = await redis.get("key")
    print(value)  # 'value'

API Reference

get_redis()

Get a Redis client with configuration management.

def get_redis(
    settings_key: str | None = None,
    *,
    cache_key: str | None = None,
    use_retry: bool | None = None,
    url: str | None = None,
    **kwargs: Any,
) -> redis.Redis:

Parameters:

  • settings_key: Configuration key to use (for multi-configuration setups)
  • cache_key: Cache key for connection pooling (defaults to URL)
  • use_retry: Enable automatic retry on connection errors
  • url: Redis connection URL (overrides configuration)
  • **kwargs: Additional parameters passed to redis.Redis.from_url()

Returns:

  • redis.Redis: Redis client instance (cached)

RedisSettings

Configuration model for Redis connection.

Fields:

  • url: SecretStr - Redis connection URL (default: "redis://localhost:6379")
  • use_retry: bool - Enable automatic retries (default: False)
  • socket_timeout: float - Socket timeout in seconds (default: 6.0)
  • socket_connect_timeout: float - Connection timeout in seconds (default: 3.0)
  • health_check_interval: int - Health check interval in seconds (default: 60)
  • retry_attempts: int - Number of retry attempts (default: 3)
  • retry_delay: float - Delay between retries in seconds (default: 1.0)
  • initialize_params: dict[str, Any] - Additional Redis client parameters

settings_manager

Global settings manager instance for Redis configuration.

from kiarina.lib.redis import settings_manager

# Configure multiple environments
settings_manager.user_config = {
    "development": {"url": "redis://localhost:6379"},
    "production": {"url": "redis://prod:6379", "use_retry": True}
}

# Switch configuration
settings_manager.active_key = "production"

Configuration

Configuration can be provided via YAML files using pydantic-settings-manager.

YAML Configuration

# config.yaml
kiarina.lib.redis:
  development:
    url: "redis://localhost:6379"
    use_retry: false

  production:
    url: "redis://prod-server:6379"
    use_retry: true
    socket_timeout: 10.0
    retry_attempts: 5
    retry_delay: 2.0

Load configuration:

import yaml
from kiarina.lib.redis import settings_manager

with open("config.yaml") as f:
    config = yaml.safe_load(f)
    settings_manager.user_config = config["kiarina.lib.redis"]

# For multi-config
settings_manager.active_key = "production"

Environment Variables

All settings can be overridden using environment variables with the KIARINA_LIB_REDIS_ prefix:

export KIARINA_LIB_REDIS_URL="redis://localhost:6379"
export KIARINA_LIB_REDIS_USE_RETRY="true"
export KIARINA_LIB_REDIS_SOCKET_TIMEOUT="10.0"

URL Formats

  • redis://localhost:6379 - Basic connection
  • redis://username:password@localhost:6379 - With authentication
  • rediss://localhost:6379 - SSL/TLS connection
  • redis://localhost:6379/0 - Specify database number
  • unix:///path/to/socket.sock - Unix socket connection

Testing

# Start Redis for testing
docker compose up -d redis

# Run tests
mise run package:test kiarina-lib-redis

# With coverage
mise run package:test kiarina-lib-redis --coverage

Dependencies

License

This project is licensed under the MIT License - see the LICENSE file for details.

Related Projects

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

kiarina_lib_redis-1.23.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

kiarina_lib_redis-1.23.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file kiarina_lib_redis-1.23.0.tar.gz.

File metadata

  • Download URL: kiarina_lib_redis-1.23.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for kiarina_lib_redis-1.23.0.tar.gz
Algorithm Hash digest
SHA256 9fd5c742e2afde2e8c8934b102122b0d85544c784d4c1b16761b1ef011b5abba
MD5 a0f0dfcb779f4d5237fbda6236f8843c
BLAKE2b-256 8cd624594835995a69c79ad27752d7190798ca291d0a9b1530acefdcd8632a45

See more details on using hashes here.

File details

Details for the file kiarina_lib_redis-1.23.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kiarina_lib_redis-1.23.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7dfbe320b5f44ba6b79b2131e2f8b8b59821b75869f3eb37a5ea4dca59a0dcdb
MD5 1dafcf9fc1c3d9dc5c7cf47cdc0b6586
BLAKE2b-256 c27f569ed45db85b9f3e77d1027895f326e19039e616ca7c2f3936d3c319acc7

See more details on using hashes here.

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