• 技术文章 >php教程 >php手册

    PHP队列用法实例,php队列实例

    2016-06-13 09:22:11原创791

    PHP队列用法实例,php队列实例


    本文实例讲述了PHP队列用法。分享给大家供大家参考。具体分析如下:

    什么是队列,是先进先出的线性表,在具体应用中通常用链表或者数组来实现,队列只允许在后端进行插入操作,在前端进行删除操作。

    什么情况下会用了队列呢,并发请求又要保证事务的完整性的时候就会用到队列,当然不排除使用其它更好的方法,知道的不仿说说看。

    队列还可以用于减轻数据库服务器压力,我们可以将不是即时数据放入到队列中,在数据库空闲的时候或者间隔一段时间后执行。比如访问计数器,没有必要即时的执行访问增加的Sql,在没有使用队列的时候sql语句是这样的,假设有5个人访问:

    update table1 set count=count+1 where id=1
    update table1 set count=count+1 where id=1
    update table1 set count=count+1 where id=1
    update table1 set count=count+1 where id=1
    update table1 set count=count+1 where id=1

    而使用队列这后就可以这样:
    update table1 set count=count+5 where id=1

    减少sql请求次数,从而达到减轻服务器压力的效果, 当然访问量不是很大网站根本没有这个必要。
    下面一个队列类:

    复制代码 代码如下:

    /**
    * 队列
    *
    * @author jaclon
    *
    */
    class Queue
    {
    private $_queue = array();
    protected $cache = null;
    protected $queuecachename;

    /**
    * 构造方法
    * @param string $queuename 队列名称
    */
    function __construct($queuename)
    {

    $this->cache =& Cache::instance();
    $this->queuecachename = 'queue_' . $queuename;

    $result = $this->cache->get($this->queuecachename);
    if (is_array($result)) {
    $this->_queue = $result;
    }
    }

    /**
    * 将一个单元单元放入队列末尾
    * @param mixed $value
    */
    function enQueue($value)
    {
    $this->_queue[] = $value;
    $this->cache->set($this->queuecachename, $this->_queue);

    return $this;
    }

    /**
    * 将队列开头的一个或多个单元移出
    * @param int $num
    */
    function sliceQueue($num = 1)
    {
    if (count($this->_queue) < $num) {
    $num = count($this->_queue);
    }
    $output = array_splice($this->_queue, 0, $num);
    $this->cache->set($this->queuecachename, $this->_queue);

    return $output;
    }

    /**
    * 将队列开头的单元移出队列
    */
    function deQueue()
    {
    $entry = array_shift($this->_queue);
    $this->cache->set($this->queuecachename, $this->_queue);

    return $entry;
    }

    /**
    * 返回队列长度
    */
    function size()
    {
    return count($this->_queue);
    }

    /**
    * 返回队列中的第一个单元
    */
    function peek()
    {
    return $this->_queue[0];
    }

    /**
    * 返回队列中的一个或多个单元
    * @param int $num
    */
    function peeks($num)
    {
    if (count($this->_queue) < $num) {
    $num = count($this->_queue);
    }
    return array_slice($this->_queue, 0, $num);
    }

    /**
    * 消毁队列
    */
    function destroy()
    {
    $this->cache->remove($this->queuecachename);
    }
    }

    希望本文所述对大家的PHP程序设计有所帮助。


    什是栈什是队列,试分别举两个应用实例

    栈是先录入的数据后输出;
    队列是先录入的数据先输出;
    这样说听得懂?例子就很难说了,很长,我们老师说了俩节课,在栈和队列上
    希望对您有帮助。


     

    用PHP实现一个双向队列

    问题不明
     

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:PHP 队列
    上一篇:7个鲜为人知却超实用的PHP函数,鲜为人知php 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• PHP类中的魔术方法(Magic Method)简明总结,magicmethod• Xgcalendar 新增Php demo• smarty模板引擎从php中获取数据的方法,smarty模板• PHP弹出提示框并跳转到新页面(重定向)• php利用新浪接口查询ip获取地理位置
    1/1

    PHP中文网