13
13
# Constants
14
14
REDIS_KEY_TTL = 3600 * 24 # 24 hour TTL as safety mechanism
15
15
16
+
16
17
def initialize ():
17
18
"""Initialize Redis connection using environment variables."""
18
19
global client
19
-
20
+
20
21
# Load environment variables if not already loaded
21
22
load_dotenv ()
22
-
23
+
23
24
# Get Redis configuration
24
25
redis_host = os .getenv ('REDIS_HOST' , 'redis' )
25
26
redis_port = int (os .getenv ('REDIS_PORT' , 6379 ))
26
27
redis_password = os .getenv ('REDIS_PASSWORD' , '' )
27
-
28
+ redis_ssl = os .getenv ('REDIS_SSL' , False )
29
+
28
30
logger .info (f"Initializing Redis connection to { redis_host } :{ redis_port } " )
29
-
31
+
30
32
# Create Redis client with basic configuration
31
33
client = redis .Redis (
32
34
host = redis_host ,
33
35
port = redis_port ,
34
36
password = redis_password ,
37
+ ssl = redis_ssl ,
35
38
decode_responses = True ,
36
39
socket_timeout = 5.0 ,
37
40
socket_connect_timeout = 5.0 ,
38
41
retry_on_timeout = True ,
39
42
health_check_interval = 30
40
43
)
41
-
44
+
42
45
return client
43
46
47
+
44
48
async def initialize_async ():
45
49
"""Initialize Redis connection asynchronously."""
46
50
global client , _initialized
47
-
51
+
48
52
async with _init_lock :
49
53
if not _initialized :
50
54
logger .info ("Initializing Redis connection" )
51
55
initialize ()
52
-
56
+
53
57
try :
54
58
await client .ping ()
55
59
logger .info ("Successfully connected to Redis" )
@@ -58,9 +62,10 @@ async def initialize_async():
58
62
logger .error (f"Failed to connect to Redis: { e } " )
59
63
client = None
60
64
raise
61
-
65
+
62
66
return client
63
67
68
+
64
69
async def close ():
65
70
"""Close Redis connection."""
66
71
global client , _initialized
@@ -71,62 +76,73 @@ async def close():
71
76
_initialized = False
72
77
logger .info ("Redis connection closed" )
73
78
79
+
74
80
async def get_client ():
75
81
"""Get the Redis client, initializing if necessary."""
76
82
global client , _initialized
77
83
if client is None or not _initialized :
78
84
await initialize_async ()
79
85
return client
80
86
87
+
81
88
# Basic Redis operations
82
89
async def set (key : str , value : str , ex : int = None ):
83
90
"""Set a Redis key."""
84
91
redis_client = await get_client ()
85
92
return await redis_client .set (key , value , ex = ex )
86
93
94
+
87
95
async def get (key : str , default : str = None ):
88
96
"""Get a Redis key."""
89
97
redis_client = await get_client ()
90
98
result = await redis_client .get (key )
91
99
return result if result is not None else default
92
100
101
+
93
102
async def delete (key : str ):
94
103
"""Delete a Redis key."""
95
104
redis_client = await get_client ()
96
105
return await redis_client .delete (key )
97
106
107
+
98
108
async def publish (channel : str , message : str ):
99
109
"""Publish a message to a Redis channel."""
100
110
redis_client = await get_client ()
101
111
return await redis_client .publish (channel , message )
102
112
113
+
103
114
async def create_pubsub ():
104
115
"""Create a Redis pubsub object."""
105
116
redis_client = await get_client ()
106
117
return redis_client .pubsub ()
107
118
119
+
108
120
# List operations
109
121
async def rpush (key : str , * values : Any ):
110
122
"""Append one or more values to a list."""
111
123
redis_client = await get_client ()
112
124
return await redis_client .rpush (key , * values )
113
125
126
+
114
127
async def lrange (key : str , start : int , end : int ) -> List [str ]:
115
128
"""Get a range of elements from a list."""
116
129
redis_client = await get_client ()
117
130
return await redis_client .lrange (key , start , end )
118
131
132
+
119
133
async def llen (key : str ) -> int :
120
134
"""Get the length of a list."""
121
135
redis_client = await get_client ()
122
136
return await redis_client .llen (key )
123
137
138
+
124
139
# Key management
125
140
async def expire (key : str , time : int ):
126
141
"""Set a key's time to live in seconds."""
127
142
redis_client = await get_client ()
128
143
return await redis_client .expire (key , time )
129
144
145
+
130
146
async def keys (pattern : str ) -> List [str ]:
131
147
"""Get keys matching a pattern."""
132
148
redis_client = await get_client ()
0 commit comments