fix redis ssl

This commit is contained in:
‘cuiran’ 2025-04-26 14:11:25 +08:00
parent 032f095bad
commit 23a2937793
1 changed files with 24 additions and 8 deletions

View File

@ -13,6 +13,7 @@ _init_lock = asyncio.Lock()
# Constants # Constants
REDIS_KEY_TTL = 3600 * 24 # 24 hour TTL as safety mechanism REDIS_KEY_TTL = 3600 * 24 # 24 hour TTL as safety mechanism
def initialize(): def initialize():
"""Initialize Redis connection using environment variables.""" """Initialize Redis connection using environment variables."""
global client global client
@ -24,6 +25,7 @@ def initialize():
redis_host = os.getenv('REDIS_HOST', 'redis') redis_host = os.getenv('REDIS_HOST', 'redis')
redis_port = int(os.getenv('REDIS_PORT', 6379)) redis_port = int(os.getenv('REDIS_PORT', 6379))
redis_password = os.getenv('REDIS_PASSWORD', '') redis_password = os.getenv('REDIS_PASSWORD', '')
redis_ssl = os.getenv('REDIS_SSL', False)
logger.info(f"Initializing Redis connection to {redis_host}:{redis_port}") logger.info(f"Initializing Redis connection to {redis_host}:{redis_port}")
@ -32,6 +34,7 @@ def initialize():
host=redis_host, host=redis_host,
port=redis_port, port=redis_port,
password=redis_password, password=redis_password,
ssl=redis_ssl,
decode_responses=True, decode_responses=True,
socket_timeout=5.0, socket_timeout=5.0,
socket_connect_timeout=5.0, socket_connect_timeout=5.0,
@ -41,6 +44,7 @@ def initialize():
return client return client
async def initialize_async(): async def initialize_async():
"""Initialize Redis connection asynchronously.""" """Initialize Redis connection asynchronously."""
global client, _initialized global client, _initialized
@ -61,6 +65,7 @@ async def initialize_async():
return client return client
async def close(): async def close():
"""Close Redis connection.""" """Close Redis connection."""
global client, _initialized global client, _initialized
@ -71,6 +76,7 @@ async def close():
_initialized = False _initialized = False
logger.info("Redis connection closed") logger.info("Redis connection closed")
async def get_client(): async def get_client():
"""Get the Redis client, initializing if necessary.""" """Get the Redis client, initializing if necessary."""
global client, _initialized global client, _initialized
@ -78,55 +84,65 @@ async def get_client():
await initialize_async() await initialize_async()
return client return client
# Basic Redis operations # Basic Redis operations
async def set(key: str, value: str, ex: int = None): async def set(key: str, value: str, ex: int = None):
"""Set a Redis key.""" """Set a Redis key."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.set(key, value, ex=ex) return await redis_client.set(key, value, ex=ex)
async def get(key: str, default: str = None): async def get(key: str, default: str = None):
"""Get a Redis key.""" """Get a Redis key."""
redis_client = await get_client() redis_client = await get_client()
result = await redis_client.get(key) result = await redis_client.get(key)
return result if result is not None else default return result if result is not None else default
async def delete(key: str): async def delete(key: str):
"""Delete a Redis key.""" """Delete a Redis key."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.delete(key) return await redis_client.delete(key)
async def publish(channel: str, message: str): async def publish(channel: str, message: str):
"""Publish a message to a Redis channel.""" """Publish a message to a Redis channel."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.publish(channel, message) return await redis_client.publish(channel, message)
async def create_pubsub(): async def create_pubsub():
"""Create a Redis pubsub object.""" """Create a Redis pubsub object."""
redis_client = await get_client() redis_client = await get_client()
return redis_client.pubsub() return redis_client.pubsub()
# List operations # List operations
async def rpush(key: str, *values: Any): async def rpush(key: str, *values: Any):
"""Append one or more values to a list.""" """Append one or more values to a list."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.rpush(key, *values) return await redis_client.rpush(key, *values)
async def lrange(key: str, start: int, end: int) -> List[str]: async def lrange(key: str, start: int, end: int) -> List[str]:
"""Get a range of elements from a list.""" """Get a range of elements from a list."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.lrange(key, start, end) return await redis_client.lrange(key, start, end)
async def llen(key: str) -> int: async def llen(key: str) -> int:
"""Get the length of a list.""" """Get the length of a list."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.llen(key) return await redis_client.llen(key)
# Key management # Key management
async def expire(key: str, time: int): async def expire(key: str, time: int):
"""Set a key's time to live in seconds.""" """Set a key's time to live in seconds."""
redis_client = await get_client() redis_client = await get_client()
return await redis_client.expire(key, time) return await redis_client.expire(key, time)
async def keys(pattern: str) -> List[str]: async def keys(pattern: str) -> List[str]:
"""Get keys matching a pattern.""" """Get keys matching a pattern."""
redis_client = await get_client() redis_client = await get_client()