RedisʹÓÃEval¶à¸ö¼üÖµ×ÔÔöµÄ²Ù×÷ʵÀý

5年以前  |  阅读数:759 次  |  编程语言:PHP 

ÔÚPHPÉÏʹÓÃRedis ¸ø¶a¸o¼uÖµ½øÐÐ×ÔÔo£¬Ê¾ÀýÈçÏ£º


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

Èç¹uÕaÑuд¿ÉÄÜÐeÒªÇeÇoÎa´Î£¬¿ÉÒÔͨ¹ý Eval ½¨À´Ò»´ÎÍe³É£¬´o´oÌa¸ßЧÂÊ


    $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 Ói¨£¬ÎÒÒ²²»ÊiϤ£¬Õa¸o×Öu´®Æ´½Ó*½Ê½Ò²ºÜÌرðµÄÊÇÁ½¸oµaºÅ ¡°..¡±


    money ..",".. ip ..",".. uv ..",".. pv
    Æ´½Óºo
    0.01,1,2,3

ÒÔÉÏÕaƪRedisʹÓÃEval¶a¸o¼uÖµ×ÔÔoµÄ²Ù×÷ʵÀý¾ÍÊÇС±a*ÖÏi¸ø´o¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍuÄܸø´o¼ÒÒ»¸o²Î¿¼£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例