《PHP编程:Redis使用Eval多个键值自增的操作实例》要点:
本文介绍了PHP编程:Redis使用Eval多个键值自增的操作实例,希望对您有用。如果有疑问,可以联系我们。
PHP实战在PHP上使用Redis 给多个键值进行自增,示例如下:
PHP实战
$set['money'] = $this->redis->hIncrByFloat($key, $hour .'_money', $data['money']);
$set['ip'] = $this->redis->hIncrBy($key, $hour .'_ip', $data['ip']);
$set['uv'] = $this->redis->hIncrBy($key, $hour .'_uv', $data['uv']);
$set['pv'] = $this->redis->hIncrBy($key, $hour .'_pv', $data['pv']);
$this->redis->hSet($key, $hour, array($data['money'], $data['ip'], $data['uv'], $data['pv']));
PHP实战如果这样写可能需要请求五次,可以通过 Eval 方法来一次完成,大大提高效率
PHP实战
$script = 'local money = redis.call("hIncrByFloat", KEYS[1], ARGV[1], ARGV[2]);';
$script .= 'local ip = redis.call("hIncrBy", KEYS[1], ARGV[3], ARGV[4]);';
$script .= 'local uv = redis.call("hIncrBy", KEYS[1], ARGV[5], ARGV[6]);';
$script .= 'local pv = redis.call("hIncrBy", KEYS[1], ARGV[7], ARGV[8]);';
$script .= 'local val = money ..",".. ip ..",".. uv ..",".. pv;';
$script .= 'redis.call("hSet", KEYS[1], ARGV[2], val);';
$script .= 'return val;';
$result = $this->redis->evaluate($script, [$key, $hour, $hour .'_money', $data['money'], $hour .'_ip', $data['ip'], $hour .'_uv', $data['uv'], $hour .'_pv', $data['pv']], 2);
var_dump($result);
PHP实战Redis 用的 Lua 语法,我也不熟悉,这个字符串拼接方式也很特别的是两个点号 “..”
PHP实战
money ..",".. ip ..",".. uv ..",".. pv
拼接后
0.01,1,2,3
PHP实战以上这篇Redis使用Eval多个键值自增的操作实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持维易PHP.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/2878.html