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

Uploaded Python 3

File details

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

File metadata

  • Download URL: kiarina_lib_redis-1.21.1.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.21.1.tar.gz
Algorithm Hash digest
SHA256 9cff88e597f3e6031d8e40ded8751a294c04b7bdb264e8c2859df298e34b456a
MD5 ff4b439e6fdb19d4661b25bf5dc986a8
BLAKE2b-256 0c743dfb40ba8438b217ebbb97e764803809dafe69efcf823d2c4de4544f5dda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kiarina_lib_redis-1.21.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d82d29e6b7005963bf465f5d9476985c1e3b7469bbaf5d3c5800d331a3dd85dd
MD5 d2eb613541646f2561777be4276e7576
BLAKE2b-256 d35b81eb21cc17c71d102de0bb0b680f14644077a81054c8f4a2f5e319cfb5c3

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