Asynchronous redis of Swoole learning

Keywords: PHP Redis OpenSSL github

I. Asynchronous redis service installation

Swoole Official Documents Guide - > Quick Start - > Asynchronous Redis client

1. swoole uses asynchronous redis preconditions

  • redis services
  • hiredis library (X)
  • Compiling swoole requires adding -- enable-async-redis (X)

Note: If your version of swoole is over 4.3, you only need to install redis service. Neither the hiredis library nor the recompilation of swoole need to be done, because version 4.3 or above is built-in.

2. Source installation redis

stay Redis official website Download the latest version locally and then decompress it.

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 decompression directory:. / 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 Installation (swoole 4.3 or more does not require installation)

The hiredis library is eventually compiled into an so file and used.

hiredis Download Address: https://github.com/redis/hire...

Use the command to download to the local location, and then decompress:

$ 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

Compile and install:

$ make -j
$ sudo make install
$ sudo ldconfig

4. recompile swoole (no recompilation is required above swoole 4.3)

Enter the swoole installation package directory:

$ cd /work/study/softpackage/swoole

View the configure parameter command:

$ ./configure --help

Show:

...

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

Why can't we find the parameter enable-async-redis here? Check the official documents of swoole, the original version of swoole 4.3 using hireidis has no need to recompile, swoole has been built-in, can be used directly. - - -!

Relevant Notes on Official Website: https://wiki.swoole.com/wiki/...

2. Code Implementation

Open redis service:

$  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);
});

Perform printing:

$ php redis.php
bool(true)
string(5) "value"

Posted by will35010 on Mon, 30 Sep 2019 21:34:28 -0700