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.13.0.tar.gz (6.1 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.13.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kiarina_lib_redis-1.13.0.tar.gz
  • Upload date:
  • Size: 6.1 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.13.0.tar.gz
Algorithm Hash digest
SHA256 2ccdf7727cd208b5e2d9b86813f44ddf4ccd4466ca2a6b87e6fac7507881e1de
MD5 70925e7cb85fced3ee61cda628b1d0b5
BLAKE2b-256 86736c36ce457fd6fa5e35a898543b9094c12472b76ccc49f307397e5eeb89d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kiarina_lib_redis-1.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9ebab58a7b65aff8c53531fa009cab633460c92db53a80997a0ab2672670939
MD5 f4686998d8c796088d8bbe791b8191d4
BLAKE2b-256 0b4a503a28a4b269861a29cae04adc3c891d47e55e990c4086ece2f34b2ff089

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