PHP中利用Redis管道加快執行

來源:互聯網
上載者:User

標籤:

$redis->muti($mode)->get($key)->set($key)->exec();
 既然是這樣的, 也就是說當我要使用管道執行一萬次操作的時候需要寫一萬次操作在muti()的後面,,,還是我找到更好的寫法?
設計者沒有想到這個問題嗎?今天測試成功了

[php] view plaincopy 
  1. <?  
  2. php   $redis = new Redis();   
  3. $redis->connect(‘10.1.132.86‘, 6379);    
  4. $pipe = $redis->multi(Redis::PIPELINE);   
  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.       
  10.     $replies = $pipe->exec(); echo " "; print_r($replies);   

 

Description: Enter and exit transactional mode.

Parameters

(optional) Redis::MULTI or Redis::PIPELINE. Defaults toRedis::MULTI. A Redis::MULTI block of commands runs as a single transaction; aRedis::PIPELINE block is simply transmitted faster to the server, but without 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 untilexec() is 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‘);*/

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

簡單資料mget也可以實現

 

mGet, getMultiple

 

Description: Get the values of all the specified keys. If one or more keys dont exist, the array will contain FALSEat the position of the key.

Parameters

Array: Array containing the list of the 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`);

PHP中利用Redis管道加快執行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.