redis管道pipeline的運用
阿新 • • 發佈:2018-07-05
redis管道 轉換成 *** put new end except rand pipe 寫入100萬條數據到指定文件
declare(strict_types=1);//開啟強類型模式 function random($length, $numeric = false) { $seed = base_convert(md5(microtime() . $_SERVER[‘DOCUMENT_ROOT‘]), 16, $numeric ? 10 : 35); $seed = $numeric ? (str_replace(‘0‘, ‘‘, $seed) . ‘012340567890‘) : ($seed . ‘zZ‘ . strtoupper($seed)); if ($numeric) { $hash = ‘‘; } else { $hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64); $length--; } $max = strlen($seed) - 1; for ($i = 0; $i < $length; $i++) { $hash .= $seed{mt_rand(0, $max)}; } return $hash; } $filePath = ‘./data.txt‘; for ($i = 0; $i <= 1000000; $i++) { $str = random(10, true); file_put_contents($filePath, $str . PHP_EOL, FILE_APPEND); }
讀取文件並寫道redis
$lines = file_get_contents($filePath);//獲取文件內容 ini_set(‘memory_limit‘, ‘-1‘);//不要限制Mem大小,否則會報錯 $arr = explode(PHP_EOL, $lines);//轉換成數組 //echo $arr[‘1000000‘] ?? ‘null‘; try { $redis = new \Redis(); $redis->connect(‘192.168.1.9‘, 6379); $redis->auth(‘*****‘);//密碼驗證 $redis->select(0);//選擇庫 $redis->pipeline();//開啟管道 foreach ($arr as $key => $value) { $redis->hsetNx(‘helloworld‘, (string)$key, $value); } $redis->exec(); echo $redis->hGet(‘helloworld‘, ‘1000000‘) . PHP_EOL; echo $redis->hGet(‘helloworld‘, ‘1000001‘) . PHP_EOL; } catch (\Exception $e) { echo $e->getMessage(); }
redis管道pipeline的運用