从文本文件中读取数据,批量插入数据到redis中
<?php
$lines = file_get_contents('test.txt');//获取文件内容
ini_set('memory_limit', '-1');//不要限制Mem大小,否则会报错
$line = explode("\r\n",$lines);//转换成数组
//实例化redis
$redis = new Redis();
//连接
$redis->connect('127.0.0.1', 6379);
$redis->pipeline();//开启管道
if(!$redis){
throw new Exception('redis连接失败!',1);
}
//$key = $info['key'];
$c = 0;
$count = count($line);
$now = time();
for($i=0;$i<$count;$i++){
echo "<Br>".$line[$i];
$value = 'ipid'.$i;
$res = $redis->hset('iptables',$line[$i],$value);
if($res){
$c ++;
}
}
$redis->exec();
redis-server
5043:M 09 May 17:54:49.038 * 100 changes in 300 seconds. Saving...
5043:M 09 May 17:54:49.039 * Background saving started by pid 6470
6470:C 09 May 17:54:49.050 * DB saved on disk
6470:C 09 May 17:54:49.051 * RDB: 6 MB of memory used by copy-on-write
5043:M 09 May 17:54:49.140 * Background saving terminated with success
5043:M 09 May 17:59:50.037 * 100 changes in 300 seconds. Saving...
5043:M 09 May 17:59:50.038 * Background saving started by pid 6584
6584:C 09 May 17:59:50.050 * DB saved on disk
6584:C 09 May 17:59:50.050 * RDB: 6 MB of memory used by copy-on-write
5043:M 09 May 17:59:50.139 * Background saving terminated with success