pip install redis
Operating System: Ubuntu 24.04.3 LTS / x86_64
Runtime Version: GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
Instance ID:Password format, custom accounts use the Username@Password format, and password-free authentication instances do not require a password.import redis# Create a connection poolpool = redis.ConnectionPool(host='192.xx.xx.2', # Instance VIP addressport=6379, # Instance portpassword='crs-xxxx:password', # Default account password format: Instance ID:Passworddb=0, # Database numbermax_connections=200, # Maximum number of connectionssocket_connect_timeout=2, # Connection timeout (in seconds)socket_timeout=2, # Read/write timeout (in seconds)decode_responses=True, # Automatically decode as stringshealth_check_interval=30 # Health check interval (in seconds))# Create a Redis clientr = redis.Redis(connection_pool=pool)# Write data.r.set('name', 'Distributed Cache')# Read data.value = r.get('name')print(f'get name: {value}')# Set a Key with an expiration timer.setex('session:user123', 3600, 'session_data')# Hash operationsr.hset('user:1001', mapping={'name': 'Zhang San', 'age': '28'})user_info = r.hgetall('user:1001')print(f'user info: {user_info}')# List operationsr.lpush('queue:tasks', 'task1', 'task2', 'task3')task = r.rpop('queue:tasks')print(f'dequeued task: {task}')# Pipeline batch operationspipe = r.pipeline()pipe.set('key1', 'value1')pipe.set('key2', 'value2')pipe.set('key3', 'value3')pipe.get('key1')pipe.get('key2')pipe.get('key3')results = pipe.execute()print(f'pipeline results: {results}')
import redis# Custom account password format: username@passwordpool = redis.ConnectionPool(host='192.xx.xx.2',port=6379,password='myuser@MyPassword', # Proxy mode custom account: the entire "username@password" string is passed as the password.db=0,max_connections=200,socket_connect_timeout=2,socket_timeout=2,decode_responses=True)r = redis.Redis(connection_pool=pool)r.set('key', 'value')print(r.get('key'))
import redis# SSL Encryption Connectionpool = redis.ConnectionPool(host='192.xx.xx.2',port=6379,password='crs-xxxx:password',db=0,max_connections=200,socket_connect_timeout=2,socket_timeout=2,decode_responses=True,ssl=True, # Enable SSL.ssl_ca_certs='/path/to/ca.pem', # Path to the CA certificatessl_cert_reqs='required' # Verify the server certificate.)r = redis.Redis(connection_pool=pool)r.set('ssl_test', 'encrypted_connection')print(r.get('ssl_test'))
import asyncioimport redis.asyncio as aioredisasync def main():# Create an asynchronous connection poolpool = aioredis.ConnectionPool(host='192.xx.xx.2',port=6379,password='crs-xxxx:password',db=0,max_connections=200,socket_connect_timeout=2,socket_timeout=2,decode_responses=True)r = aioredis.Redis(connection_pool=pool)# Asynchronous writeawait r.set('async_key', 'async_value')# Asynchronous readvalue = await r.get('async_key')print(f'async get: {value}')# Asynchronous Pipelinepipe = r.pipeline()pipe.set('key1', 'value1')pipe.set('key2', 'value2')pipe.get('key1')pipe.get('key2')results = await pipe.execute()print(f'async pipeline: {results}')# Close the connection.await r.aclose()await pool.aclose()asyncio.run(main())
Parameter | Description | Recommended Value |
host | Instance connection address. | - |
port | Instance port number | 6379 |
password | Authentication password | Default account format: Instance ID:Password |
max_connections | Maximum Number of Connections | 200 |
socket_connect_timeout | Connection timeout (s) | 2 |
socket_timeout | Read/write timeout (s) | 2 |
health_check_interval | Health check interval (s) | 30 |
decode_responses | Automatically decode responses into strings | True |
retry_on_timeout | Automatic retry on timeout | True |
max_connections is set appropriately.피드백