lSize("Code_Active_list"); for($i=0;$i< $num;$i++){ $redis->rPop("Code_Active_list"); } $status=1; break; case 2: $num = $redis->lSize("Code_Add_list"); for($i=0;$i< $num;$i++){ $redis->rPop("Code_Add_list"); } $status=1; break; case 3: S('fenbiao_page',Null); $status=1; break; default: $status=2; } if($status==1){ echo '清空成功'; } else{ echo '清空失败'; } } /*查款redis里面的数据*/ public function getS() { if(empty(I('flag'))){ echo '请输入操作动作'; } $redis=new Redis(); $flag=I('flag'); switch ($flag){ case 1: echo '待更新二维码:'. $redis->lSize("Code_Active_list"); break; case 2: echo '待新增二维码:'. $redis->lSize("Code_Add_list"); break; case 3: echo '待分表二维码:'. $redis->lSize("fenbiao_page"); break; default: $status=2; } } public function togetTxtActivefile(){ /* $redis=new Redis(); $redis->rm('Code_Active_list'); echo '待更新二维码:'. $redis->lSize("Code_Active_list");die;*/ $result = array(); if(empty(I('active_batch'))){ $result['status'] = 0; $result['cont'] = '激活批次号缺少'; $this->ajaxReturn($result); } /*修改激活批次表*/ $return1 = M('activate_list')-> where('active_batch ='.I('active_batch'))->setField('status',1); $return2 = $this->getTxtActivefile(I('active_batch')); /*修改激活批次表*/ if($return1 && $return2){ $result['status'] = 1; $result['cont'] = '任务已提交'; $this->ajaxReturn($result); } } /* * 获取需要激活的二维码 * 并插入到redis 缓存中去 * 默认 需要插入的目录Data/active/ * 激活批次号:active_batch */ public function getTxtActivefile($active_batch){ /*$redis=new Redis(); echo '待更新二维码:'. $redis->lSize("Code_Active_list"); die; $active_batch = 991156;*/ set_time_limit(0); $this->active_batch = $active_batch; $dir="Data/Code_Active_list/".$active_batch.'/0/'; $files=scandir($dir); $txt_file_array=array(); $redis=new Redis(); foreach($files as $key ){ if(get_extension($key)=='txt'){ //更新缓存 $this->CodeStatusUpdateCache($key,'Code_Active_list','http://hn.6in7.cn/'); } } /*修改表数据*/ $return = M('activate_list')-> where('active_batch ='.$this->active_batch)->setField('status',0); return true; //echo '待更新二维码:'. $redis->lSize("Code_Active_list"); } public function togetTxtAddfile(){ $result = array(); if( empty(I('bach_id')) || empty(I('tasks_id')) || empty(I('product_id')) || empty(I('printer_id')) || empty(I('task_name'))){ $result['status'] = 0; $result['cont'] = '批次参数缺少'; $this->ajaxReturn($result); }else{ $this->tasks_id = I('tasks_id'); $this->bach_id = I('bach_id'); $this->product_id = I('product_id'); $this->printer_id = I('printer_id'); $this->task_name = I('task_name'); } /*修改任务表*/ $return1 = M('task')-> where('id ='.I('tasks_id'))->setField('lock',1); $return2 = $this->getTxtAddfile(); /*修改激活批次表*/ if($return1 && $return2){ $result['status'] = 1; $result['cont'] = '任务已提交'; $this->ajaxReturn($result); } } /* * 该函数需要后期后端操作,一个文件要对应 任务id,产品id,批次号等绑定 * 获取需要新增的二维码 * 并插入到redis 缓存中去 * 默认 需要插入的目录Data/add/ * */ public function getTxtAddfile(){ $dir="Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/'; $files=scandir($dir); $txt_file_array=array(); $redis=new Redis(); foreach($files as $key ){ if(get_extension($key)=='txt'){ //更新缓存 $this->CodeStatusUpdateCache($key,'Code_Add_list','http://hn.6in7.cn/'); } } $return = M('task')-> where('id ='.I('tasks_id'))->setField('lock',0); return true; } /* * 二维码插入 * redis主键 Code_Add_list * Code_Add_list每一条数据为serialize 数据, * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s * 一分钟访问两次 执行时间在30s~60s * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m */ public function addCode(){ $redis=new Redis(); if(!$redis->lSize("Code_Add_list")){ echo '没有需要插入的二维码数据'; } $i=0;$j=0; // $CodeMode->startTrans(); w_log('Data/Add_log.txt',time()." : 插入开始"); $t1 = microtime(true); //echo $redis->lSize("Code_Add_list").'
'; //die; for($kk=5000;$kk>0 && $redis->lSize("Code_Add_list")>0;$kk--){ $where=array(); $where=unserialize($redis->rPop("Code_Add_list")) ; $where['add_time']=time(); $where['update_time']=time(); $where['varify_code']=''; if(strpos($where['code'], ',')){ $temp_code=explode(',', $where['code']); $where['code']=current($temp_code); $where['varify_code']=end($temp_code); } $task_name = $where['task_name']; $tasks_id = $where['tasks_id']; $file = $where['file']; unset($where['file']); unset($where['task_name']); $where_str=''; $field_str=''; foreach ( $where as $key=>$value){ if($key=='varify_code'){ $field_str.="`".$key."`"; $where_str.="'".$value."'"; }else{ $field_str.="`".$key."`,"; $where_str.="'".$value."',"; } } $table_name= getSubTable('codes',$where['code']); $table_name = "INSERT ignore INTO `$table_name` ($field_str) VALUES ($where_str)"; $Model = M(); $status = $Model->execute($table_name); if($status) { $i++; M('task')->where('id = '.$tasks_id)->setInc('checked_num',1); } else{ //插入错误日志 $j++; $dir = "Data/Code_Add_list/".$task_name.'/admin/'.$task_name.'_'.$tasks_id.'/2/'; if(!file_exists($dir)){ //判断文件是否存在 mkdir($dir, 0777, true); } w_log($dir.$file,time().':'.$where['code'].$where['varify_code'].'添加失败'); /*错误记录表 +1*/ M('task')->where('id = '.$tasks_id)->setInc('wrong_num',1); } } $t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒
'; echo '成功插入:'.$i.'条数据,失败:'.$j.'条数据
'; } /* * 二维码更新状态变更 * redis主键 Code_Active_list * 平均单条插别人耗时间:0.0045s,单次插入数量:5000;预计耗时:22.5s~30s * 一分钟访问两次 执行时间在30s~60s * 一分钟处理1w条数据,100w 平均耗时 100分钟=1 h 40 m */ public function activeCode(){ $redis=new Redis(); if(!$redis->lSize("Code_Active_list")){ echo '没有需要更新的二维码数据'; return 'ok'; } w_log('Data/Active_log.txt',time()."更新开始"); $t1 = microtime(true); $i=0;$j=0; $time = time(); for($kk=5000;$kk>0;$kk--){ $where=array(); $Active_data=unserialize($redis->rPop("Code_Active_list")); $where['code']=$Active_data['code']; if(strpos($where['code'], ',')){ $where['code']=current(explode(',', $where['code'])); } $where['Status']=0; $data=array(); $data['Status']=1; $data['active_batch']=$Active_data['active_batch']; $data['update_time']= $time; $data['active_time']= $time; if($data['active_batch']){ $table_name= getSubTable('codes',$where['code'],1); $CodeMode= M($table_name); $status=$CodeMode->where($where)->save($data); if($status) { $i++; //修改激活批次表 M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('succ',1); } else{ //0代表码错误,1代表重复激活,2代表系统错误 $j++; //写日志 $insert_data = array(); $insert_data['code'] = $where['code']; $insert_data['active_batch'] = $data['active_batch']; $insert_data['time'] = $time; $return1 = $CodeMode->field('id')->where('code = '.'"'.$where['code'].'"')->select(); if(count($return1)>0){ $return2 = $CodeMode->field('id')->where($where)->find(); if(count($return2)>0){ $insert_data['error'] = 2; }else{ $insert_data['error'] = 1; } }else{ $insert_data['error'] = 0; } if( M('activate_list_log')->add($insert_data)){ M('activate_list')->where('active_batch = '.$data['active_batch'])->setInc('fail',1); } //判断开始 //w_log('Data/Active_wrong_'.$data['active_batch'].'_log.txt',time().':'.$where['code'].'更新失败'); } }else{ $j++; w_log('Data/Active_wrong_log.txt',time().':'.$where['code'].'更新失败,激活批次为空'); } } $t2 = microtime(true); echo '耗时'.round($t2-$t1,3).'秒
'; echo '更新成功:'.$i.'条数据,失败:'.$j.'条数据
'; echo '还剩'.$redis->lSize("Code_Active_list").'条数据待更新
'; w_log('Data/Active_log.txt',time().'更新结束 共插入 条数据'); } /* * redis 操作 * 把需要更新的二维码 * push 到 Code_Active_list主键 * & redis_key redis主键 * 二维码中包含的主域名 */ protected function CodeStatusUpdateCache($file,$redis_key='Code_Active_list',$host='http://hn.6in7.cn/'){ if($redis_key=='Code_Active_list'){ $dir = 'Data/'.$redis_key.'/'.$this->active_batch.'/0/'.$file; $path = 'Data/'.$redis_key.'/'.$this->active_batch.'/1/'.$file; }else{ $dir = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/0/'.$file; $path = "Data/Code_Add_list/".$this->task_name.'/'.get_current_admin_name() . '/'.$this->task_name.'_'.$this->tasks_id.'/1/'.$file; } $cont= file_get_contents(iconv("utf-8","gbk",$dir)); $cont = preg_replace('/\n|\r\n/','*',$cont); $cont =str_replace($host,"",$cont); $data = explode("*",$cont); $data = array_filter($data); $total = count($data); //写入日志 w_log('Data/redis_log.txt',time().":".$file."文件插入Code_Active_list 队列中"); $redis=new Redis(); $i=0; foreach($data as $k=>$v){ /* * 如果是插入需要对数据重组 */ if($redis_key=='Code_Add_list'){ /*相关的参数 start*/ $Insert_data['code']=$v; $Insert_data['bach_id']=$this->bach_id; $Insert_data['tasks_id']=$this->tasks_id; $Insert_data['product_id']=$this->product_id; $Insert_data['printer_id']=$this->printer_id; $Insert_data['file'] = $file; $Insert_data['task_name'] = $this->task_name; $Insert_data['Status']=0; /*相关的参数 stop*/ $redis->lpush($redis_key,serialize($Insert_data)); }else if($redis_key=='Code_Active_list'){ $Active_data['code']=$v; $Active_data['active_batch']=$this->active_batch; $redis->lpush($redis_key,serialize($Active_data)); } else{ $redis->lpush($redis_key,$v); } $i++; } copy($dir, $path); unlink($dir); w_log('Data/redis_log.txt',time().":".$file."共有数据:".$total.',已插入数据:'.$i.',{$redis_key} 目前共有数据:'.$redis->lSize($redis_key)); } /* * 获取分表表名 */ }