zoukankan      html  css  js  c++  java
  • Redis 使用 Eval 多个键值自增操作示例

    在PHP上使用Redis 给多个键值进行自增,示例如下:

    $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']));

    如果这样写可能需要请求五次,可以通过 Eval 方法来一次完成,大大提高效率

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

    Redis 用的 Lua 语法,我也不熟悉,这个字符串拼接方式也很特别的是两个点号 “..”

    money ..",".. ip ..",".. uv ..",".. pv
    拼接后
    0.01,1,2,3
  • 相关阅读:
    DOCK-SWARM
    springMVC上传和下载文件
    生成任意内容任意类型的文件
    配置监听(系统启动和关闭时运行的程序)
    配置过滤器
    读取文件内容(TXT之类的文件)
    读取properties文件中的内容
    时间戳转时间字符串和时间
    启动Service的时候报错unable to install breakpoint in
    java后台调用接口并获取返回值
  • 原文地址:https://www.cnblogs.com/zhouzme/p/5758408.html
Copyright © 2011-2022 走看看