Skip to content

Commit 0d420ee

Browse files
authored
Merge pull request #139 from cayden/main
fix redis ssl
2 parents 032f095 + 23a2937 commit 0d420ee

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

backend/services/redis.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,47 @@
1313
# Constants
1414
REDIS_KEY_TTL = 3600 * 24 # 24 hour TTL as safety mechanism
1515

16+
1617
def initialize():
1718
"""Initialize Redis connection using environment variables."""
1819
global client
19-
20+
2021
# Load environment variables if not already loaded
2122
load_dotenv()
22-
23+
2324
# Get Redis configuration
2425
redis_host = os.getenv('REDIS_HOST', 'redis')
2526
redis_port = int(os.getenv('REDIS_PORT', 6379))
2627
redis_password = os.getenv('REDIS_PASSWORD', '')
27-
28+
redis_ssl = os.getenv('REDIS_SSL', False)
29+
2830
logger.info(f"Initializing Redis connection to {redis_host}:{redis_port}")
29-
31+
3032
# Create Redis client with basic configuration
3133
client = redis.Redis(
3234
host=redis_host,
3335
port=redis_port,
3436
password=redis_password,
37+
ssl=redis_ssl,
3538
decode_responses=True,
3639
socket_timeout=5.0,
3740
socket_connect_timeout=5.0,
3841
retry_on_timeout=True,
3942
health_check_interval=30
4043
)
41-
44+
4245
return client
4346

47+
4448
async def initialize_async():
4549
"""Initialize Redis connection asynchronously."""
4650
global client, _initialized
47-
51+
4852
async with _init_lock:
4953
if not _initialized:
5054
logger.info("Initializing Redis connection")
5155
initialize()
52-
56+
5357
try:
5458
await client.ping()
5559
logger.info("Successfully connected to Redis")
@@ -58,9 +62,10 @@ async def initialize_async():
5862
logger.error(f"Failed to connect to Redis: {e}")
5963
client = None
6064
raise
61-
65+
6266
return client
6367

68+
6469
async def close():
6570
"""Close Redis connection."""
6671
global client, _initialized
@@ -71,62 +76,73 @@ async def close():
7176
_initialized = False
7277
logger.info("Redis connection closed")
7378

79+
7480
async def get_client():
7581
"""Get the Redis client, initializing if necessary."""
7682
global client, _initialized
7783
if client is None or not _initialized:
7884
await initialize_async()
7985
return client
8086

87+
8188
# Basic Redis operations
8289
async def set(key: str, value: str, ex: int = None):
8390
"""Set a Redis key."""
8491
redis_client = await get_client()
8592
return await redis_client.set(key, value, ex=ex)
8693

94+
8795
async def get(key: str, default: str = None):
8896
"""Get a Redis key."""
8997
redis_client = await get_client()
9098
result = await redis_client.get(key)
9199
return result if result is not None else default
92100

101+
93102
async def delete(key: str):
94103
"""Delete a Redis key."""
95104
redis_client = await get_client()
96105
return await redis_client.delete(key)
97106

107+
98108
async def publish(channel: str, message: str):
99109
"""Publish a message to a Redis channel."""
100110
redis_client = await get_client()
101111
return await redis_client.publish(channel, message)
102112

113+
103114
async def create_pubsub():
104115
"""Create a Redis pubsub object."""
105116
redis_client = await get_client()
106117
return redis_client.pubsub()
107118

119+
108120
# List operations
109121
async def rpush(key: str, *values: Any):
110122
"""Append one or more values to a list."""
111123
redis_client = await get_client()
112124
return await redis_client.rpush(key, *values)
113125

126+
114127
async def lrange(key: str, start: int, end: int) -> List[str]:
115128
"""Get a range of elements from a list."""
116129
redis_client = await get_client()
117130
return await redis_client.lrange(key, start, end)
118131

132+
119133
async def llen(key: str) -> int:
120134
"""Get the length of a list."""
121135
redis_client = await get_client()
122136
return await redis_client.llen(key)
123137

138+
124139
# Key management
125140
async def expire(key: str, time: int):
126141
"""Set a key's time to live in seconds."""
127142
redis_client = await get_client()
128143
return await redis_client.expire(key, time)
129144

145+
130146
async def keys(pattern: str) -> List[str]:
131147
"""Get keys matching a pattern."""
132148
redis_client = await get_client()

0 commit comments

Comments
 (0)