@@ -115,22 +115,6 @@ func NewRedisCache(options RedisCacheOptions) (*RedisCache, error) {
115115 Protocol : 2 , // Use RESP2 protocol for compatibility
116116 })
117117
118- // Test connection
119- ctx := context .Background ()
120- if config .Connection .Timeout > 0 {
121- timeout := time .Duration (config .Connection .Timeout ) * time .Second
122- var cancel context.CancelFunc
123- ctx , cancel = context .WithTimeout (ctx , timeout )
124- defer cancel ()
125- logging .Debugf ("RedisCache: connection timeout set to %s" , timeout )
126- }
127-
128- if err := redisClient .Ping (ctx ).Err (); err != nil {
129- logging .Debugf ("RedisCache: failed to connect: %v" , err )
130- return nil , fmt .Errorf ("failed to connect to Redis: %w" , err )
131- }
132- logging .Debugf ("RedisCache: successfully connected to Redis" )
133-
134118 cache := & RedisCache {
135119 client : redisClient ,
136120 config : config ,
@@ -140,6 +124,13 @@ func NewRedisCache(options RedisCacheOptions) (*RedisCache, error) {
140124 enabled : options .Enabled ,
141125 }
142126
127+ // Test connection using the new CheckConnection method
128+ if err := cache .CheckConnection (); err != nil {
129+ logging .Debugf ("RedisCache: failed to connect: %v" , err )
130+ return nil , err
131+ }
132+ logging .Debugf ("RedisCache: successfully connected to Redis" )
133+
143134 // Set up the index for vector search
144135 logging .Debugf ("RedisCache: initializing index '%s'" , config .Index .Name )
145136 if err := cache .initializeIndex (); err != nil {
@@ -350,6 +341,31 @@ func (c *RedisCache) IsEnabled() bool {
350341 return c .enabled
351342}
352343
344+ // CheckConnection verifies the Redis connection is healthy
345+ func (c * RedisCache ) CheckConnection () error {
346+ if ! c .enabled {
347+ return nil
348+ }
349+
350+ if c .client == nil {
351+ return fmt .Errorf ("redis client is not initialized" )
352+ }
353+
354+ ctx := context .Background ()
355+ if c .config != nil && c .config .Connection .Timeout > 0 {
356+ timeout := time .Duration (c .config .Connection .Timeout ) * time .Second
357+ var cancel context.CancelFunc
358+ ctx , cancel = context .WithTimeout (ctx , timeout )
359+ defer cancel ()
360+ }
361+
362+ if err := c .client .Ping (ctx ).Err (); err != nil {
363+ return fmt .Errorf ("redis connection check failed: %w" , err )
364+ }
365+
366+ return nil
367+ }
368+
353369// AddPendingRequest stores a request that is awaiting its response
354370func (c * RedisCache ) AddPendingRequest (requestID string , model string , query string , requestBody []byte ) error {
355371 start := time .Now ()
0 commit comments