# Swoole\Redis\Server
Swoole-1.8.14版本增加一個兼容Redis服務器端協議的Server框架,可基于此框架實現Redis協議的服務器程序。Swoole\Redis\Server繼承自Swoole\Server,可調用父類提供的所有方法。
Redis\Server不需要設置onReceive回調。實例程序:https://github.com/swoole/swoole-src/blob/master/examples/redis/server.php
**可用的客戶端**
* 任意編程語言的redis客戶端,包括PHP的redis擴展和phpredis庫
* Swoole擴展提供的異步Redis客戶端
* Redis提供的命令行工具,包括redis-cli、redis-benchmark
**方法**
Swoole\Redis\Server繼承自Swoole\Server,可以使用父類提供的所有方法。
Redis\Server不需要設置onReceive回調。只需使用setHandler方法設置對應命令的處理函數,收到未支持的命令后會自動向客戶端發送ERROR響應,消息為ERR unknown command '$command'。
**setHandler**
設置Redis命令字的處理器。
~~~
function swoole_redis_server->setHandler(string $command, callable $callback);
~~~
* $command 命令的名稱
* $callback 命令的處理函數,回調函數返回字符串類型時會自動發送給客戶端
* $callback 返回的數據必須為Redis格式,可使用format靜態方法進行打包
服務器實例
~~~
use Swoole\Redis\Server;
$server = new Server('127.0.0.1', 9501);
//同步模式
$server->setHandler('Set', function($fd, $data) {
$server->array($data[0], $data[1]);
return Server::format(Server::INT, 1);
});
//異步模式
$server->setHandler('Get', function ($fd, $data) use ($server) {
$db->query($sql, function($db, $result) use ($fd) {
$server->send($fd, Server::format(Server::LIST, $result));
});
});
$server->start();
~~~
客戶端實例
~~~
redis-cli -h 127.0.0.1 -p 9501 set name rango
~~~
**format**
格式化命令響應數據。
~~~
function swoole_redis_server::format(int $type, mixed $value = null);
~~~
* format為靜態方法
* $type表示數據類型,NIL類型不需要傳入$value,ERROR和STATUS類型$value可選,INT、STRING、SET、MAP必選
- 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