php队列下载文件,PHP队列框架WanQueue/文件/redis队列

PHP队列框架WanQueue/文件队列,可快速切换到reids/mysql等任何队列。开心/方便/简单。

运行 php WanQueue/console/QueueServer.php 开启队列服务

//添加任务到队列

$que=new WanQueue\Queue\Queue();

$que->push(new WanQueue\Jobs\Email('18618300482@163.com','i love wanwan','i love you !'));

1.[代码]使用示例

//自动加载自己写

//添加队列任务

$que=new WanQueue\Queue\Queue();

$que->push(new WanQueue\Jobs\Email('18618300482@163.com','i love wanwan','i love you !'));

2.[代码]Job示例

namespace WanQueue\Jobs;

/**

* Created by PhpStorm.

* User: wanwan

* Date: 16/2/27

* Time: 下午3:33

*/

class Email implements Job{

private $email='';

private $title='';

private $content='';

function __construct($email,$title,$content)

{

$this->email=$email;

$this->title=$title;

$this->content=$content;

}

function handle()

{

// TODO: Implement handle() method.

echo 'email to :',$this->email,' theme : ',$this->title ,' send success !',"\n";

}

}

3.[代码]redis驱动队列,新手也可以使用队列哟!为PHPer谋福利

namespace WanQueue\Queue;

/**

* Created by PhpStorm.

* User: wanwan

* Date: 16/2/24

* Time: 上午10:33

*/

class Queue{

private $queue=null;

private $queueName='';

/**

* Queue constructor. 加载队列

* @param string $queueName 队列名称

*/

function __construct($queueName='Queue')

{

$this->queueName=$queueName;

$this->getRedisQueue();

}

/**

* 获取队列---redis版

*/

private function getRedisQueue(){

if($this->queue==null){

try {

$this->queue = new \Redis();

$this->queue->pconnect('127.0.0.1', 6379);

} catch (Exception $e) {

file_put_contents(date('Y-m-d').'-redis.log', date('Y-m-d H:i:s').' : '. $e->getMessage(), FILE_APPEND);

}

}

}

/**

* 获取队列-----文件版

*/

private function getQueue(){

$queue=__DIR__."/Queue/{$this->queueName}.queue";

if(!is_file($queue)){

return [];

}

return json_decode(file_get_contents($queue),true);

}

/**

* 队列持久化----文件版

*/

private function setQueue(){

if(!is_dir(__DIR__."/Queue")){

mkdir(__DIR__."/Queue");

}

file_put_contents(__DIR__."/Queue/{$this->queueName}.queue",json_encode($this->queue));

}

/**

* 入队

* @param $value

* @return int

*/

function push($value){

return $this->queue->rpush($this->queueName,serialize($value));

}

/**

* 出队

* @return mixed

*/

function pop(){

return unserialize($this->queue->lpop($this->queueName));

}

/**

* 从开始入队

* @param $value

* @return int

*/

function prePush($value){

return $this->queue->lpush($this->queueName,serialize($value));

}

/**

* 从末尾出队

* @return mixed

*/

function popL(){

return unserialize($this->queue->rpop($this->queueName));

}

/**

* 队列保存

*/

function __destruct()

{

// TODO: Implement __destruct() method.

//$this->setQueue();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值