connect($host, $port, 2.0)) { throw new \RuntimeException("Could not connect to Redis server at $host:$port"); } // Authenticate if password is provided if ($password) { if (!$redis->auth($password)) { throw new \RuntimeException("Redis authentication failed."); } } // Set prefix for Saqel to avoid collisions $redis->setOption(\Redis::OPT_PREFIX, 'saqel:'); self::$instance = $redis; } catch (\Exception $e) { // In production, fallback gracefully or throw HTTP 500 error_log("Redis Connection Error: " . $e->getMessage()); throw new \RuntimeException("Redis is unavailable. Please ensure the Redis server is running."); } } return self::$instance; } /** * Helper to close the connection explicitly */ public static function close(): void { if (self::$instance !== null) { self::$instance->close(); self::$instance = null; } } }