1、增加了一个扩展类Queue.php
2、代码里改了一下tp自带的redis类,增加了下面几个方法
/**
* 插入队列
* @param $k
* @param $value
* @return bool
* @author sunnier <xiaoyao_xiao@126.com>
*/
public function push($k,$value){
$value = (is_object($value) || is_array($value)) ? json_encode($value) : $value;
$result=$this->handler->lpush($this->options['prefix'].$k,$value);
if($result){
return true;
}else{
return false;
}
}
/**
* 弹出队列
* @param $name
* @return array|mixed
* @author sunnier <xiaoyao_xiao@126.com>
*/
public function pop($name,$sendtimeout=0){
if(empty($sendtimeout)){
$timeout=$this->options['timeout'];
}else{
$timeout=$sendtimeout;
}
$keys=array($this->options['prefix'].$name);
$value = $this->handler->brPop($keys,$timeout);
$jsonData = json_decode( $value[1], true );
return ($jsonData === NULL) ? $value[1]: $jsonData;
}
/**
* 获取队列长度
* @param $name
* @return int
* @author sunnier <xiaoyao_xiao@126.com>
*/
public function len($name){
return $this->handler->lLen($this->options['prefix'].$name);
}
/**
* 删除队列
* @param $name
* @return int
* @author sunnier <xiaoyao_xiao@126.com>
*/
public function del($name){
return $this->handler->del($this->options['prefix'].$name);
}
3、写了一个function中的方法用来发送邮件function think_send_mail($to, $name, $subject = '', $body = '', $attachment = null){
//插入到邮件表中,并插入redis等待发送
$mid=session('uid');
if(empty($mid)){
$mid=0;
}
$data=array(
'member_id'=>$mid,
'email'=>$to,
'send_name'=>$name,
'title'=>$subject,
'content'=>$body,
'send_times'=>1,
'create_time'=>time(),
);
$emailSendLog=M('EmailSendLog');
if($id=$emailSendLog->add($data)){
//添加成功之后插入redis
vendor('Queue');
$data['id']=$id;
$queque=\Think\My\Queue::getInstance('emailsend');
if($queque->myPush($data)){
return true;
}else{
return false;
}
}else{
//echo $emailSendLog->getError();
return false;
}
}
4、重新写了一个cli.php用来使用命令行模式的tp执行控制器ScheduleController.class.php5、crontab的配置见图,其中有一个code参数是get传输的值,自己随便改,主要是防止方法被乱调用