PHP隊列用法執行個體,php隊列執行個體_PHP教程

來源:互聯網
上載者:User

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實現一個雙向隊列

問題不明
 

http://www.bkjia.com/PHPjc/906670.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/906670.htmlTechArticlePHP隊列用法執行個體,php隊列執行個體 本文執行個體講述了PHP隊列用法。分享給大家供大家參考。具體分析如下: 什麼是隊列,是先進先出的線性表,...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.