codecamp

Swoole Redis Server实例

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-cliredis-benchmark
注意:Swoole-1.8.0版本增加了对异步Redis客户端的支持,基于redis官方提供的hiredis库实现。Swoole提供了__call魔术方法,来映射绝大部分Redis指令。

编译安装hiredis

使用Redis客户端,需要安装hiredis库。下载hiredis源码后,执行

make -j
sudo make install
sudo ldconfig

启用异步Redis客户端

编译swoole是,在configure指令中加入--enable-async-redis

./configure --enable-async-redis
make clean
make -j
sudo make install

简单实例:

$redis = new Swoole\Redis;
$redis->connect('127.0.0.1', 6379, function ($redis, $result) {
    $redis->set('test_key', 'value', function ($redis, $result) {
        $redis->get('test_key', function ($redis, $result) {
            var_dump($result);
        });
    });
});

$cli = new Swoole\Http\Client('127.0.0.1', 80);
$cli->setHeaders(array('User-Agent' => 'swoole-http-client'));
$cli->setCookies(array('test' => 'value'));

$cli->post('/dump.php', array("test" => 'abc'), function ($cli) {
    var_dump($cli->body);
    $cli->get('/index.php', function ($cli) {
        var_dump($cli->cookies);
        var_dump($cli->headers);
    });
});

Swoole WebSocket实例
Swoole MySQL实例
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }