# redis client
異步Redis客戶端
[TOC=2,3]
Swoole-1.8.0版本增加了對異步Redis客戶端的支持,基于redis官方提供的hiredis庫實現。Swoole提供了__call魔術方法,來映射絕大部分Redis指令。
## **編譯安裝hiredis**
使用Redis客戶端,需要安裝hiredis庫。下載hiredis源碼后,執行
~~~
make -j
sudo make install
sudo ldconfig
~~~
* hiredis下載地址:https://github.com/redis/hiredis/releases
## **啟用異步Redis客戶端**
編譯swoole是,在configure指令中加入--enable-async-redis
~~~
./configure --enable-async-redis
make clean
make -j
sudo make install
~~~
## **swoole_redis->__construct**
Redis異步客戶端構造方法,可以設置Redis連接的配置選項。
~~~
function swoole_redis->__construct(array $options = null);
~~~
* $options 配置選項數組,默認為null
* 在1.9.15或更高版本可用
**超時控制**
~~~
$options['timeout'] = 1.5;
~~~
浮點型,單位為秒,最小粒度為1毫秒。Connect后,在規定的時間內服務器沒有完成握手,底層將自動關閉socket,設置連接為失敗,觸發onConnect事件
**設置密碼**
~~~
$options['password'] = 'passwd';
~~~
必須為字符串類型,可以設置Redis服務器密碼,等同于auth指令
**設置數據庫**
~~~
$options['database'] = 0;
~~~
* 整型,設置使用的Redis服務器的數據庫編號,等同于select指令
>設置了password或database選項后,連接就緒后底層會自動發送相關指令
>等待服務器響應成功后才會觸發onConnect連接成功事件
>如果password或database錯誤,onConnect連接結果為失敗
## **swoole_redis->on**
注冊事件回調函數。
~~~
function swoole_redis->on(string $event_name, callable $callback);
~~~
目前swoole_redis支持2種事件回調函數。on方法必須在connect前被調用。
**onClose**
當Redis服務器主動關閉連接或者客戶端主動調用close關閉連接時,會觸發onClose事件。
~~~
function onClose(swoole_redis $redis);
~~~
**onMessage**
當客戶端收到來自服務器的訂閱消息時觸發onMessage事件。
~~~
function onMessage(swoole_redis $redis, array $message);
~~~
## **swoole_redis->connect**
連接到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);
});
});
~~~
## **swoole_redis->__call**
魔術方法,方法名會映射為Redis指令,參數作為Redis指令的參數。
**函數原型**
~~~
function swoole_redis->__call(string $command, array $params);
~~~
* $command,必須為合法的Redis指令,詳細參見Redis指令列表
* $params的最后一個參數必須為可執行的函數,其他參數必須為字符串
**訂閱/發布消息**
Redis服務器除了作為內存存儲之外,還可以作為一個消息通道服務器。SwooleRedis客戶端也支持了Redis的訂閱/發布消息指令。
與普通的存儲指令不同,消息訂閱/發布指令不是請求響應式的。
* 訂閱/發布指令沒有回調函數,不需要在最后一個參數傳入callback
* 使用訂閱/發布消息命名,必須設置onMessage事件回調函數
* 客戶端發出了subscribe命令后,只能執行subscribe, psubscribe,unsubscribe,punsubscribe這4條命令
~~~
$client = new swoole_redis;
$client->on('message', function (swoole_redis $client, $result) {
var_dump($result);
static $more = false;
if (!$more and $result[0] == 'message')
{
echo "subscribe new channel\n";
$client->subscribe('msg_1', 'msg_2');
$client->unsubscribe('msg_0');
$more = true;
}
});
$client->connect('127.0.0.1', 6379, function (swoole_redis $client, $result) {
echo "connect\n";
$client->subscribe('msg_0');
});
~~~
**回調函數**
~~~
function onReceive(swoole_redis $redis, bool $result);
~~~
* $redis: redis連接對象
* 執行失敗,$result為false, 可以讀取$redis->errCode獲得錯誤碼,讀取$redis->errMsg獲得錯誤消息
* 執行成功,返回數據結果,可能是字符串、數組或true
**使用示例**
~~~
$client->get('key', function (swoole_redis $client, $result) {
var_dump($result);
});
~~~
## **swoole_redis->close**
關閉Redis連接,不接受任何參數。
~~~
function swoole_redis->close()
~~~
異步redis客戶端
```php
$client = new Redis;
$client->connect('127.0.0.1', 6379, function (Redis $client, $result) {
echo "connect\n";
var_dump($result);
$db = 0;
$client->select($db);
$password = '111111';
$client->auth($password);
});
$client->set('key', 'swoole', function (Redis $client, $result) {
var_dump($result);
$client->get('key', function (Redis $client, $result) {
var_dump($result);
$client->close();
});
});
```
- swoole簡介
- swoole功能概述
- 序章
- 開發必讀
- 1 環境搭建
- 1.1 環境搭建
- 1.2 搭建Echo服務器
- 2 初識Swoole
- 2.1 Worker進程
- 2.2 TaskWorker進程
- 2.3 Timer定時器
- 2.4 Process進程
- 2.5 Table內存表
- 2.6 多端口監聽
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 熱重啟
- 2.10 http_server
- 附錄*server配置
- 附錄*server函數
- 附錄*server屬性
- 附錄*server回調函數
- 附錄*server高級特性
- 心跳檢測
- 3 Swoole協議
- 3.1 EOF協議
- 3.2 固定包頭協議
- 3.3 Http協議
- 3.4 WebSocket協議
- 3.5 MTQQ協議
- 內置http_server
- 內置websocket_server
- Swoole\Redis\Server
- 4 Swoole異步IO
- 4.1 AsyncIO
- 異步文件系統IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole異步客戶端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole協程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定時器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常見問題
- 客戶端鏈接失敗原因
- 如何設置進程數
- 如何實現異步任務
- 如何選擇swoole三種模式
- php中哪些函數是阻塞的
- 是否可以共用1個redis或mysql連接
- 如何在回調函數中訪問外部的變量
- 為什么不要send完后立即close
- 不同的Server程序實例間如何通信
- MySQL的連接池、異步、斷線重連
- 在php-fpm或apache中使用swoole
- 學習Swoole需要掌握哪些基礎知識
- 在phpinfo中有在php-m中沒有
- 同步阻塞與異步非阻塞選擇
- CURL發送POST請求服務器端超時
- 附錄
- 預定義常量
- 內核參數調優
- php四種回調寫法
- 守護進程程序常用數據結構
- swoole生命周期
- swoole_server中內存管理機制
- 使用jemalloc優化swoole內存分配性能
- Reactor、Worker、Task的關系
- Manager進程
- Swoole的實現
- Reactor線程
- 安裝擴展
- swoole-worker手冊
- swoole相關開源項目
- 寫在后面的話
- 版本更新記錄
- 4.0