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)
インスタンスID:パスワード形式を使用し、カスタムアカウントはユーザー名@パスワード形式を使用します。パスワード不要の認証インスタンスはパスワードが不要です。import redis接続プールを作成します。pool = redis.ConnectionPool(host='192.xx.xx.2', # インスタンスのVIPアドレスport=6379, # インスタンスポートpassword='crs-xxxx:パスワード', # デフォルトアカウントのパスワード形式:インスタンスID:パスワードdb=0, # データベース番号max_connections=200, # 最大接続数socket_connect_timeout=2, # 接続タイムアウト(秒)socket_timeout=2, # 読み書きタイムアウト(秒)decode_responses=True, # 文字列に自動デコードします。health_check_interval=30 # ヘルスチェック間隔(秒))Redisクライアントを作成します。r = redis.Redis(connection_pool=pool)# データを書き込みます。r.set('name', 'Distributed Cache')# データを読み取ります。value = r.get('name')print(f'get name: {value}')# 有効期限付きのKeyを設定します。r.setex('session:user123', 3600, 'session_data')# ハッシュ操作r.hset('user:1001', mapping={'name': '張三', 'age': '28'})user_info = r.hgetall('user:1001')print(f'user info: {user_info}')# リスト操作r.lpush('queue:tasks', 'task1', 'task2', 'task3')task = r.rpop('queue:tasks')print(f'dequeued task: {task}')# Pipeline バッチ操作pipe = 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# カスタムアカウントのパスワード形式:ユーザー名@パスワードpool = redis.ConnectionPool(host='192.xx.xx.2',port=6379,password='myuser@MyPassword', # プロキシモードのカスタムアカウント:ユーザー名@パスワード全体をパスワードとして渡します。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暗号化接続pool = redis.ConnectionPool(host='192.xx.xx.2',port=6379,password='crs-xxxx:パスワード',db=0,max_connections=200,socket_connect_timeout=2,socket_timeout=2,decode_responses=True,ssl=True, # SSLを有効にします。ssl_ca_certs='/path/to/ca.pem', # CA証明書のパスssl_cert_reqs='required' # サーバー証明書を検証します。)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():# 非同期接続プールを作成します。pool = aioredis.ConnectionPool(host='192.xx.xx.2',port=6379,password='crs-xxxx:パスワード',db=0,max_connections=200,socket_connect_timeout=2,socket_timeout=2,decode_responses=True)r = aioredis.Redis(connection_pool=pool)# 非同期書き込みawait r.set('async_key', 'async_value')# 非同期読み取りvalue = await r.get('async_key')print(f'async get: {value}')# 非同期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}')# 接続を閉じます。await r.aclose()await pool.aclose()asyncio.run(main())
パラメータ | 説明 | 推奨値 |
host | インスタンス接続アドレス。 | - |
port | インスタンスポート番号 | 6379 |
password | 認証パスワード | デフォルトアカウント形式: インスタンスID:パスワード |
max_connections | 最大接続数 | 200 |
socket_connect_timeout | 接続タイムアウト(秒) | 2 |
socket_timeout | 読み書きタイムアウト(秒) | 2 |
health_check_interval | ヘルスチェック間隔(秒) | 30 |
decode_responses | レスポンスを自動的に文字列へデコードします。 | True |
retry_on_timeout | タイムアウト時に自動的に再試行します。 | True |
max_connectionsが適切に設定されているかどうかを確認してください。피드백