@@ -3665,54 +3665,49 @@ def _connect_cache(self) -> bool:
36653665 if self ._cache_connection_status in ['redis_ok' , 'sqlite_ok' ]:
36663666 return True
36673667
3668- # 2. The "Off Switch": User has explicitly disabled Redis in config.
3669- if self .config .get ('redis_enabled' , True ) is False :
3670- # We haven't tried SQLite yet, so proceed to the SQLite block.
3671- pass # Let execution fall through to the SQLite connection logic.
3672-
3673- # 3. Redis Attempt (only if not disabled and not already failed)
3674- elif self ._cache_connection_status != 'redis_failed' :
3675- try :
3676- # The "Golden Rule": Default to localhost if no host is specified.
3677- redis_host = self .config .get ('redis_host' , 'localhost' )
3678- redis_port = self .config .get ('redis_port' , 6379 )
3679-
3680- # Use a very aggressive timeout. 0.2 seconds is plenty.
3681- cache_client = redis .Redis (
3682- host = redis_host ,
3683- port = redis_port ,
3684- decode_responses = True ,
3685- socket_connect_timeout = 0.2 , # Extremely fast timeout
3686- socket_timeout = 0.2
3687- )
3688- cache_client .ping ()
3689-
3690- self .cache_client = cache_client
3691- self ._cache_connection_status = 'redis_ok' # SUCCESS! Remember this.
3692- safe_print ('⚡️ Connected to Redis successfully (High-performance mode).' )
3693- return True
3694-
3695- except redis .exceptions .ConnectionError :
3696- safe_print ('⚠️ Redis not found or offline. Falling back to local SQLite cache.' )
3697- self ._cache_connection_status = 'redis_failed' # FAILURE! Remember this.
3698- except Exception as e :
3699- safe_print (f'⚠️ Redis connection failed: { e } . Falling back to SQLite.' )
3700- self ._cache_connection_status = 'redis_failed' # FAILURE! Remember this.
3668+ # 2. THE CRITICAL FIX: Check if Redis is even installed BEFORE trying to use it.
3669+ if REDIS_AVAILABLE and self .config .get ('redis_enabled' , True ) is True :
3670+ # This block now ONLY runs if the 'redis' library was successfully imported.
3671+
3672+ # 3. Redis Attempt (only if not already failed)
3673+ if self ._cache_connection_status != 'redis_failed' :
3674+ try :
3675+ redis_host = self .config .get ('redis_host' , 'localhost' )
3676+ redis_port = self .config .get ('redis_port' , 6379 )
3677+
3678+ cache_client = redis .Redis (
3679+ host = redis_host ,
3680+ port = redis_port ,
3681+ decode_responses = True ,
3682+ socket_connect_timeout = 0.2 ,
3683+ socket_timeout = 0.2
3684+ )
3685+ cache_client .ping ()
3686+
3687+ self .cache_client = cache_client
3688+ self ._cache_connection_status = 'redis_ok'
3689+ safe_print ('⚡️ Connected to Redis successfully (High-performance mode).' )
3690+ return True
3691+
3692+ except redis .exceptions .ConnectionError :
3693+ safe_print ('⚠️ Redis not found or offline. Falling back to local SQLite cache.' )
3694+ self ._cache_connection_status = 'redis_failed'
3695+ except Exception as e :
3696+ safe_print (f'⚠️ Redis connection failed: { e } . Falling back to SQLite.' )
3697+ self ._cache_connection_status = 'redis_failed'
37013698
3702- # 4. SQLite Fallback (runs if Redis is disabled, failed , or not installed )
3699+ # 4. SQLite Fallback (runs if Redis is disabled, not installed , or failed to connect )
37033700 try :
37043701 sqlite_db_path = self .config_manager .config_dir / f'cache_{ self .env_id } .sqlite'
37053702 self .cache_client = SQLiteCacheClient (sqlite_db_path )
37063703 if not self .cache_client .ping ():
37073704 raise RuntimeError ('SQLite connection failed ping test.' )
37083705
3709- self ._cache_connection_status = 'sqlite_ok' # SUCCESS! Remember this.
3706+ self ._cache_connection_status = 'sqlite_ok'
37103707
3711- # Only print the "Using SQLite" message on the first successful connection.
3712- # This prevents redundant messages on subsequent calls.
3713- if self .cache_client is not None and not hasattr (self , '_sqlite_message_printed' ):
3714- safe_print (f'✅ Using local SQLite cache at: { sqlite_db_path } ' )
3715- self ._sqlite_message_printed = True # Prevent this message from printing again.
3708+ if not hasattr (self , '_sqlite_message_printed' ):
3709+ safe_print (f'✅ Using local SQLite cache.' )
3710+ self ._sqlite_message_printed = True
37163711
37173712 return True
37183713
0 commit comments