企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# swoole\_redis->connect [TOC] 连接到Redis服务器 ## 函数原型: ~~~ function swoole_redis->connect(string $host, int $port, callable $callback); ~~~ * $host: Redis服务器的主机IP * $port: Redis服务器的端口 * $callback: 连接成功后回调的函数 ## 回调函数 ~~~ function onConnect(swoole_redis $redis, bool $result); ~~~ * $redis: redis连接对象 * $result: 连接成功为true,连接失败为false,可以读取`$redis->errCode`获得错误码,读取`$redis->errMsg`获得错误消息 连接成功后就可以执行Redis指令了。 ## 使用示例 ~~~ $client = new swoole_redis; $client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) { if ($result === false) { echo "connect to redis server failed.\n"; return; } $client->set('key', 'swoole', function (swoole_redis $client, $result) { var_dump($result); }); }); ~~~