關於Swoole安裝hiredis無法使用異步redis客戶端問題
問題描述
根據swoole官方文檔安裝hiredis並重新編譯swoole加入–enable-async-redis後依然無法使用redis擴展。編寫並運行異步redis文件,會有如下錯誤提示:
Fatal error: Uncaught Error: Class 'swoole_redis' not found in xxx
Fatal error: Uncaught Error: Class 'Swoole\Redis' not found in xxx
問題原因
這是由於從版本4.3.0開始,異步客戶端和API被移到單獨的PHP擴展 swoole_async 了。
官方文檔有說明:傳送門
解決方案
- 安裝swoole異步async擴展
git clone https://github.com/swoole/ext-async.git cd ext-async phpize //注意,如果安裝了多個版本的PHP,這裏phpize應該是當前使用PHP版本對應的phpize全路徑 ./configure make -j 4 sudo make install
- 增加擴展配置到php.ini
vim /usr/local/php/lib/php.ini extension=swoole_async.so
- 測試異步redis是否可用(異步模塊(redis等)只能在cli模式下使用,官方文檔有說明)
執行該文件,結果:<?php //$redisClient = new swoole_redis; $redisClient = new Swoole\Redis; $redisClient->connect('127.0.0.1', 6379, function(swoole_redis $redisClient, $result){ echo 'connect:'.PHP_EOL; var_dump($result); }); echo 'start';
此時說明,swoole異步模塊擴展已經安裝完成。