py-redis (Redis Client Wrapper)

1.1.1 · active · verified Thu Apr 16

py-redis is a convenience wrapper for the official `redis-py` package, simplifying Redis client instantiation. It allows configuring the client via environment variables (REDIS_HOST, REDIS_PORT, REDIS_DB, REDIS_PASSWORD) and provides a single function to get a pre-configured `redis.Redis` instance. The current version is 1.1.1, and its release cadence is infrequent as it primarily provides a thin stable wrapper.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to get a Redis client using `get_redis_client`, leveraging environment variables for configuration. It then performs a basic `set` and `get` operation, ensuring `decode_responses=True` for string handling.

import os
from pyredis import get_redis_client

# Configure Redis connection via environment variables for robustness
# Example: REDIS_HOST=localhost REDIS_PORT=6379 REDIS_DB=0 REDIS_PASSWORD=""

# Fallback to defaults if env vars are not set
redis_host = os.environ.get('REDIS_HOST', 'localhost')
redis_port = int(os.environ.get('REDIS_PORT', '6379'))
redis_db = int(os.environ.get('REDIS_DB', '0'))
redis_password = os.environ.get('REDIS_PASSWORD') or None

try:
    # Get a configured Redis client instance
    # decode_responses=True is highly recommended for working with strings
    redis_client = get_redis_client(
        host=redis_host,
        port=redis_port,
        db=redis_db,
        password=redis_password,
        decode_responses=True
    )

    # Test the connection
    redis_client.ping()
    print("Successfully connected to Redis!")

    # Perform a simple operation
    redis_client.set('mykey', 'Hello, py-redis!')
    value = redis_client.get('mykey')
    print(f"Retrieved from Redis: {value}")

    redis_client.delete('mykey') # Clean up

except Exception as e:
    print(f"Could not connect to Redis or an error occurred: {e}")
    print("Please ensure Redis server is running and connection details are correct.")

view raw JSON →