Swoole学习之异步redis(八)
一、异步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"