Redis instance _php using eval multiple key values self-increment

Source: Internet
Author: User
Tags redis

Use Redis on PHP to increase the number of key values, as shown in the following example:

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

If you may need to ask five times to write this, you can do this one at a time by using the Eval method, greatly improving your efficiency

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

I'm not familiar with the Lua syntax for Redis, and this string concatenation is also very special with two dots "..."

Money ... ",". IP ... ",".. UV ... ",".. PV
splicing after
0.01,1,2,3

The above Redis uses the Eval multiple key value self increment operation example is small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.