一、异步 redis 服务安装
Swoole 官方文档入门指引 -> 快速起步 -> 异步 Redis 客户端
1、swoole 使用异步 redis 前置条件
- redis 服务
- hiredis 库(
X
) - 编译 swoole 需要加入
--enable-async-redis
(X
)
注意 :如果您的 swoole 版本为 4.3 以上,则只需要安装 redis 服务即可,hiredis 库和重新编译 swoole 都不需要在做了,因为 4.3 以上版本已经内置了。
2、源码安装 redis
在 Redis 官网下载最新版本到本地,然后解压缩。
Download, extract and compile Redis with:
$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar xzf redis-5.0.5.tar.gz
$ cd redis-5.0.5
$ make
The binaries that are now compiled are available in the src directory. Run Redis with:
redis 解压缩目录:./redis-5.0.5/src/redis-server
$ src/redis-server
You can interact with Redis using the built-in client:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
3、hiredis 安装 (swoole4.3 以上不需要安装)
hiredis 库最终编译为一个 so 文件,然后去使用它。
hiredis 下载地址:https://github.com/redis/hire…
使用命令下载到本地,然后解压缩:
$ wget https://github.com/redis/hiredis/archive/v0.14.0.tar.gz
$ mv v0.14.0.tar.gz hiredis-v0.14.0.tar.gz
$ tar xzf hiredis-v0.14.0.tar.gz
$ cd hiredis-0.14.0
编译安装:
$ make -j
$ sudo make install
$ sudo ldconfig
4、重新编译 swoole(swoole4.3 以上不需要重新编译)
进入 swoole 安装包目录:
$ cd /work/study/softpackage/swoole
查看 configure 的参数命令:
$ ./configure --help
显示:
...
Optional Features and Packages:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]
--with-PACKAGE[=ARG] use PACKAGE [ARG=yes]
--without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no)
--with-libdir=NAME Look for libraries in .../NAME rather than .../lib
--with-php-config=PATH Path to php-config php-config
--enable-debug-log Enable swoole debug log
--enable-trace-log Enable swoole trace log
--enable-sockets Do you have sockets extension?
--enable-openssl Use openssl?
--enable-http2 Use http2.0?
--enable-swoole Enable swoole support
--enable-mysqlnd Do you have mysqlnd?
--with-openssl-dir=DIR Include OpenSSL support (requires OpenSSL >= 0.9.6)
--with-jemalloc-dir=DIR Include jemalloc support
--enable-asan Enable asan
--enable-gcov Enable gcov
--enable-debug, compile with debug symbols
--enable-shared=PKGS Build shared libraries default=yes
--enable-static=PKGS Build static libraries default=yes
--enable-fast-install=PKGS
Optimize for fast installation default=yes
--with-gnu-ld Assume the C compiler uses GNU ld default=no
--disable-libtool-lock Avoid locking (might break parallel builds)
--with-pic Try to use only PIC/non-PIC objects default=use both
--with-tags=TAGS Include additional configurations automatic
这里怎么没有找到
--enable-async-redis
这个参数呢?查了一下 swoole 官方文档,原来从 swoole4.3 版本使用 hireidis 已经不需要再重新编译了,swoole 已经内置了,直接使用即可。-_-!
官网相关说明:https://wiki.swoole.com/wiki/…
二、代码实现
开启 redis 服务:
$ src/redis-server
reids.php
<?php
const REDIS_SERVER_HOST = '127.0.0.1';
const REDIS_SERVER_PORT = 6379;
go(function () {$redis = new Swoole\Coroutine\Redis();
$redis->connect(REDIS_SERVER_HOST, REDIS_SERVER_PORT);
$redis->setDefer();
$redis->set('key1', 'value');
$redis2 = new Swoole\Coroutine\Redis();
$redis2->connect(REDIS_SERVER_HOST, REDIS_SERVER_PORT);
$redis2->setDefer();
$redis2->get('key1');
$result1 = $redis->recv();
$result2 = $redis2->recv();
var_dump($result1, $result2);
});
执行打印:
$ php redis.php
bool(true)
string(5) "value"