Swoole-src: 协程 Redis 客户端 hincrby 方法返回值和 phpredis 的结果不一致

Created on 17 May 2018  ·  5Comments  ·  Source: swoole/swoole-src

文档中说“方法的使用基本与phpredis保持一致”。

正常时候是发现
phpredis 的 hincrby方法返回的是 key=>val 格式的

swoole的协程redis客户端返回的是 [ key1,val1,key2,val2... ] 格式的

question

Most helpful comment

    public function handleResult($method, $arguments, $result) {
        if ($method == 'zrank' && $result === null) {
            return false;
        }

        if ($method == 'get' && $result === null) {
            return false;
        }

        if (empty($result)) {
            return $result;
        }

        if ($method == 'hmget') {
            $keys = array_values($arguments[1]);
            $rs = array();
            foreach ($result as $k => $v) {
                $rs[$keys[$k]] = $v;
            }
            $result = $rs;
        } else if (
            $method == 'hgetall' ||
            (in_array($method, array('zrange', 'zrevrange')) && $arguments[3] == true) ||
            (in_array($method, array('zrangebyscore', 'zrevrangebyscore')) && isset($arguments[3]) && $arguments[3]['withscores'])
        ) {
            $keys = array_chunk($result, 2);
            $rs = array();
            foreach ($keys as $v) {
                $rs[$v[0]] = $v[1];
            }
            $result = $rs;
        }

        return $result;
    }

@cabal-php 根据我的不完全测试,上面的兼容语法基本可以满足你的要求

All 5 comments

hincrby? 是不是打错了, 这个方法返回的不是数字吗?

$redis->delete('h');
$redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */
$redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
    public function handleResult($method, $arguments, $result) {
        if ($method == 'zrank' && $result === null) {
            return false;
        }

        if ($method == 'get' && $result === null) {
            return false;
        }

        if (empty($result)) {
            return $result;
        }

        if ($method == 'hmget') {
            $keys = array_values($arguments[1]);
            $rs = array();
            foreach ($result as $k => $v) {
                $rs[$keys[$k]] = $v;
            }
            $result = $rs;
        } else if (
            $method == 'hgetall' ||
            (in_array($method, array('zrange', 'zrevrange')) && $arguments[3] == true) ||
            (in_array($method, array('zrangebyscore', 'zrevrangebyscore')) && isset($arguments[3]) && $arguments[3]['withscores'])
        ) {
            $keys = array_chunk($result, 2);
            $rs = array();
            foreach ($keys as $v) {
                $rs[$v[0]] = $v[1];
            }
            $result = $rs;
        }

        return $result;
    }

@cabal-php 根据我的不完全测试,上面的兼容语法基本可以满足你的要求

不好意思 写错了 是 hgetall ,难道是版本原因?我之前2.1.3

使用 4.1.0 的 Swoole\Runtime::enableCoroutine() 可以将 redis 扩展协程化。

Co\Redis compatibility mode

Co\Redis 的函数 hmGet/hGetAll/zrange/zrevrange/zrangebyscore/zrevrangebyscore 返回结果与php不一致的问题,已经得到解决 #2529。为了兼容老版本,在加上 $redis->setOptions(['compatibility_mode' => true]); 配置后,即可保证 Co\Redis 和 PHP Redis 返回结果一致。

go(function() {
    $redis = new Swoole\Coroutine\Redis();
    $redis->setOptions(['compatibility_mode' => true]);
    $redis->connect(REDIS_SERVER_HOST, REDIS_SERVER_PORT);

    $co_get_val = $redis->get('novalue');
    $co_zrank_val = $redis->zRank('novalue', 1);
    $co_hgetall_val = $redis->hGetAll('hkey');
    $co_hmget_val = $redis->hmGet('hkey', array(3, 5));
    $co_zrange_val = $redis->zRange('zkey', 0, 99, true);
    $co_zrevrange_val = $redis->zRevRange('zkey', 0, 99, true);
    $co_zrangebyscore_val = $redis->zRangeByScore('zkey', 0, 99, ['withscores' => true]);
    $co_zrevrangebyscore_val = $redis->zRevRangeByScore('zkey', 99, 0, ['withscores' => true]);
});
Was this page helpful?
0 / 5 - 0 ratings

Related issues

nick-zh picture nick-zh  ·  3Comments

pthreat picture pthreat  ·  3Comments

rovico picture rovico  ·  4Comments

andreybolonin picture andreybolonin  ·  4Comments

chenye2017 picture chenye2017  ·  3Comments