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

Uploaded Python 3

File details

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

File metadata

  • Download URL: kiarina_lib_redis-1.25.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.25.0.tar.gz
Algorithm Hash digest
SHA256 4c00a6ee20e8278931d10034663b53b6665f5156986a341e8d9176684baf4d8f
MD5 f135a126f111efce14db79bfafd25464
BLAKE2b-256 299bdefe82f36856a30d2089d68de30837609bceb0a29571eb0ace7bec84255e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kiarina_lib_redis-1.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2fc2107ff69176e34cb6dab156776bc489e8ae27477890f100072324604143
MD5 ddc976a230ed6262eaea94cd80c3063c
BLAKE2b-256 d4461ecc1e092ff2275a7b1b3be61c666d9f0d96f0844d1ea115ed39366f5af5

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