{"library":"types-redis","code":"import redis\nfrom typing import Optional, Any\nimport os\n\ndef get_redis_client() -> redis.Redis[Any]:\n    # In a real application, you'd get connection details from environment variables or a configuration system.\n    # Using os.environ.get for quickstart, assuming Redis is running locally on default port.\n    host = os.environ.get('REDIS_HOST', 'localhost')\n    port = int(os.environ.get('REDIS_PORT', 6379))\n    db = int(os.environ.get('REDIS_DB', 0))\n    \n    print(f\"Connecting to Redis at {host}:{port}/{db}\")\n    # types-redis provides the type hints for the Redis client.\n    return redis.Redis(host=host, port=port, db=db, decode_responses=True)\n\ndef set_and_get_value(client: redis.Redis[Any], key: str, value: str) -> Optional[str]:\n    client.set(key, value)\n    result: Optional[str] = client.get(key)\n    print(f\"Set '{key}':'{value}', Retrieved: '{result}'\")\n    return result\n\ndef increment_counter(client: redis.Redis[Any], key: str) -> int:\n    initial_value = client.setnx(key, 0) # Set if not exists, returns 1 if set, 0 otherwise\n    current_count: int = client.incr(key)\n    print(f\"Incremented '{key}' to {current_count}\")\n    return current_count\n\nif __name__ == \"__main__\":\n    # Ensure a Redis server is running, e.g., via docker: docker run -p 6379:6379 -it redis:latest\n    try:\n        r_client = get_redis_client()\n        r_client.ping() # Test connection\n        print(\"Successfully connected to Redis!\")\n\n        user_id_key: str = \"user:profile:101\"\n        user_data: str = \"{'name': 'Jane Doe', 'email': 'jane.doe@example.com'}\"\n        set_and_get_value(r_client, user_id_key, user_data)\n\n        page_views_key: str = \"app:metrics:page_views\"\n        increment_counter(r_client, page_views_key)\n        increment_counter(r_client, page_views_key)\n\n        # Example of using a typed command return value\n        all_keys: list[str] = r_client.keys('user:*')\n        print(f\"Found user keys: {all_keys}\")\n\n    except redis.exceptions.ConnectionError as e:\n        print(f\"Could not connect to Redis: {e}. Please ensure Redis server is running.\")\n    except Exception as e:\n        print(f\"An unexpected error occurred: {e}\")\n    finally:\n        # Clean up example keys (optional)\n        if 'r_client' in locals() and r_client.ping(): # Check if connected before deleting\n            r_client.delete(user_id_key, page_views_key)\n            print(\"Cleaned up example keys.\")","lang":"python","description":"This quickstart demonstrates how to initialize a synchronous Redis client and perform basic operations like setting/getting values and incrementing a counter. The type hints are automatically provided by `types-redis` for the `redis` library. Note that `decode_responses=True` is often used for string handling, making the type hints more directly applicable to Python strings.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}