Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php 队列类 消息队列 thinkphp 队列 php队列处理高并

php 队列类 消息队列 thinkphp 队列 php队列处理高并

WBOY
WBOYasal
2016-07-29 08:52:053084semak imbas

<code><span><span><?php </span><span>/** 
 * PHP Class for queue 
 *<span> @author</span> yangqijun@live.cn 
 *<span> @copyright</span> DataFrog Beijingbei  Ltd. 2011-07-25 
 */</span><span><span>class</span><span>Queue</span> {</span><span>public</span><span>$length</span>=<span>12</span>;          <span>//默认队列,相当于初始化队列</span><span>public</span><span>$queue</span>  = <span>array</span>();  <span>//  if String like this "22,23,24"    convert to array to do queue  </span><span>public</span><span>$delimiter</span>=<span>','</span>;  

    <span><span>function</span><span>__construct</span><span>(<span>$queue</span>=array<span>()</span>)</span>    {</span><span>$this</span>->queue=<span>$queue</span>;
    }  
    <span>/** 
     *<span> @desc</span> start queue 
     *<span> @param</span> String  $param  new queue element 
     */</span><span>public</span><span><span>function</span><span>run</span><span>(<span>$param</span>)</span>    {</span><span>if</span>(!is_array(<span>$this</span>->queue)){  
            <span>$this</span>->strToQue();<span>//将数组视为队列 </span>
        }  
        <span>$currentlength</span>=<span>$this</span>->countqueue();        <span>//Count  the  queue length</span><span>echo</span><span>$currentlength</span>;
        <span>echo</span><span>$this</span>->length.<span>'<br>'</span>;   
        <span>if</span>(<span>$currentlength</span>$this</span>->length&&<span>$this</span>->length><span>0</span>) {  
            <span>$this</span>->queAdd(<span>$param</span>);  
        }<span>else</span><span>if</span>(<span>$this</span>->length==<span>0</span>)  <span>//如果为空队列,则将队列初始化为输入的队列</span>
        {  
            <span>$param</span>=<span>empty</span>(<span>$param</span>)?<span>0</span>:<span>$param</span>;  
            <span>$this</span>->queue[]=<span>$param</span>;  
        }  
        <span>else</span> {              
            <span>for</span> (<span>$i</span>=<span>0</span>;<span>$i</span>$currentlength</span>-<span>$this</span>->length-<span>1</span>;<span>$i</span>++)<span>//队列比规定的队列多,要删掉队首的元素,才能入队</span>
            {  
                <span>$this</span>->queRemove();  
            }  
            <span>$this</span>->queAdd(<span>$param</span>);  
        }               
        <span>return</span><span>$this</span>->queue;               
    }  
    <span>/** 
     * String like this "22,23,24"  convert to array to do queue 
     *<span> @param</span> String $string 
     *<span> @param</span> String $delimiter 
     */</span><span>public</span><span><span>function</span><span>strToQue</span><span>()</span>{</span><span>if</span> (<span>empty</span>(<span>$this</span>->queue))  
        {  
            <span>$this</span>->queue=<span>array</span>();  
        }  
        <span>else</span>        {  
            <span>$this</span>->queue=explode(<span>$this</span>->delimiter,<span>$this</span>->queue);  
        }       
    }  
    <span>/** 
     * insert $node into queue 
     *<span> @param</span> string $node 
     */</span><span>private</span><span><span>function</span><span>queAdd</span><span>(<span>$node</span>)</span>{</span>        array_push(<span>$this</span>->queue,<span>$node</span>);  
        <span>$this</span>->countqueue();  
    }  
    <span>private</span><span><span>function</span><span>queRemove</span><span>()</span>{</span><span>$node</span> = array_shift(<span>$this</span>->queue);  
        <span>$this</span>->countqueue();  
        <span>return</span><span>$node</span>;  
    }  
    <span>private</span><span><span>function</span><span>countqueue</span><span>()</span>{</span><span>$currentlength</span>= count(<span>$this</span>->queue);  
        <span>return</span><span>$currentlength</span>;  
    }  
    <span><span>function</span><span>__destruct</span><span>()</span>    {</span><span>unset</span>(<span>$this</span>->queue);  
    }  
}   
<span>//example  </span><span>$str</span>=<span>'88|89|90|56|23|45|69|23|20|100'</span>; 
<span>$obj</span>=<span>new</span> Queue (<span>$str</span>); 
<span>$obj</span>->length=<span>8</span>;  <span>// 队列元素长度  </span><span>$obj</span>->delimiter=<span>'|'</span>;  <span>//如果队列是字符串,则元素直接的分隔符为| </span><span>$a</span>=<span>$obj</span>->run(<span>'91'</span>);   <span>//要添加到队列中的元素 </span><span>$a</span>=<span>$obj</span>->run(<span>'92'</span>);  
<span>$a</span>=<span>$obj</span>->run(<span>'93'</span>);  
<span>$a</span>=<span>$obj</span>->run(<span>'94'</span>);  
print_r(<span>$a</span>);
<span>?></span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介绍了php 队列类,包括了php,队列方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn