Faster execution with Redis pipeline in PHP

Source: Internet
Author: User
Tags vars

$redis->muti ($mode)->get ($key)->set ($key)->exec ();
Since this is the case, that is, when I want to use the pipeline to perform 10,000 operations, I need to write 10,000 operations at the back of Muti (),,, or do I find a better way of writing?
Didn't the designer think of that? The test was successful today.

[PHP]View Plaincopy
  1. <?
  2. php $redis = new Redis ();
  3. $redis->connect (' 10.1.132.86 ', 6379);
  4. $pipe = $redis->multi (redis::P ipeline);
  5. for ($i = 0; $i < 10000; $i + +) {   
  6. $pipe->set ("key:: $i", str_pad ($i, 4, ' 0 ', 0));
  7. $pipe->get ("key:: $i");
  8. }
  9. $replies = exec () $pipe; echo "";   Print_r ($replies);

Description: Enter and exit transactional mode.

Parameters

(optional) Redis::MULTI or Redis::PIPELINE . Defaults to Redis::MULTI . A Redis::MULTI block of commands runs as a single transaction; a Redis::PIPELINE block was simply transmitted faster to the server, but wit Hout any guarantee of atomicity. discard Cancels a transaction.

Return Value

multi()Returns the Redis instance and enters Multi-mode. Once in Multi-mode, all subsequent method calls return the same object until is exec() called.

Example
$ret = $redis->multi()    ->set(‘key1‘, ‘val1‘)    ->get(‘key1‘)    ->set(‘key2‘, ‘val2‘)    ->get(‘key2‘)    ->exec();/*$ret == array(    0 => TRUE,    1 => ‘val1‘,    2 => TRUE,    3 => ‘val2‘);*/

##############

Simple data mget can also be implemented

MGet, GetMultiple

Description: Get The values of all the specified keys. If one or more keys dont exist, the array would contain at the position of the FALSE key.

Parameters

array: Array containing the list of keys

Return Value

array: Array containing the values related to keys in argument

Examples
$redis->set(‘key1‘, ‘value1‘);$redis->set(‘key2‘, ‘value2‘);$redis->set(‘key3‘, ‘value3‘);$redis->mGet(array(‘key1‘, ‘key2‘, ‘key3‘)); /* array(‘value1‘, ‘value2‘, ‘value3‘);$redis->mGet(array(‘key0‘, ‘key1‘, ‘key5‘)); /* array(`FALSE`, ‘value2‘, `FALSE`);

Faster execution with Redis pipeline in PHP

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.