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.26.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.26.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kiarina_lib_redis-1.26.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.26.0.tar.gz
Algorithm Hash digest
SHA256 ce1233a81676c5bc585240fecfce753f30f0c907ae27f8ca0dfda57efb585571
MD5 f4c23cc1c0ea33de41a4e530c57795b2
BLAKE2b-256 40cbf21c3bddd76e4c9c37904d3c98141ad1b924118ef9f56de7e19cc0c8efd6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kiarina_lib_redis-1.26.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2200c03bc298d46be878ab298c4b34a5a3ba7c6c13a0f277b0073d9df06dc353
MD5 487c8175134b9a10f96f7689de1a9b9c
BLAKE2b-256 88983ad78533bc1c448083de496af06dcf2ae380a36e0e67c8435c1bf18f1aa8

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