首页 > php教程 > PHP源码 > 正文

PHP队列框架WanQueue/文件/redis队列

PHP中文网
发布: 2016-05-23 08:39:15
原创
2140人浏览过

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 !'));

使用方法

<?php
 
//自动加载自己写
 
//添加队列任务
$que=new WanQueue\Queue\Queue();
$que->push(new WanQueue\Jobs\Email('18618300482@163.com','i love wanwan','i love you !'));
登录后复制

job示例

序列猴子开放平台
序列猴子开放平台

具有长序列、多模态、单模型、大数据等特点的超大规模语言模型

序列猴子开放平台 0
查看详情 序列猴子开放平台
<?php
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";
    }
 
}
登录后复制

redis驱动队列

<?php
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();
    }
}
登录后复制
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号