PHP佇列是什麼
PHP具有非常強大的功能,所有的CGI的功能PHP都能實現,而且支援幾乎所有流行的資料庫以及作業系統。最重要的是PHP可以用C、C++進行程式的擴充套件!今天,小編為大家搜尋整理了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關於訪問控制的和運算子優先順序介紹,希望對你能有所幫助。程式碼如下:classFoo{private$name='hdj';publicfunctiongetName(){return$this->name;}}classBarextendsFoo{pub...
-
php二維陣列相同鍵名相加例項
導語:在php二維陣列雷同鍵名相加,你寫嗎?下面的是本站小編為大家蒐集的php二維陣列相同鍵名相加例項,希望對你會有所幫助。Array([uid]=>19[pid]=>Array([0]=>91[1]=>81)[price]=>Array([0]=>6[1]=>14)[pnum]=>Array([0]=>1[1]=>1))求相同鍵名的值相加如(price的相...
-
PHP開發環境安裝的方法
PHP是一種HTML內嵌式的語言,是一種在伺服器端執行的嵌入HTML文件的指令碼語言,語言的風格有類似於C語言,被廣泛地運用。以下是小編為大家搜尋整理的PHP開發環境安裝的方法,歡迎閱讀!更多精彩內容請及時關注我們應屆畢業生考試網!一、PHP簡介PHP於1994年由RasmusLerdo...
-
PHP考試筆試題
用寶珠打扮自己,不如用知識充實自己。以下是小編為大家搜尋整理的PHP考試筆試題,希望能給大家帶來幫助!更多精彩內容請及時關注我們應屆畢業生考試網!1、{PHP題目}識別符號是變數的名稱。PHP中的識別符號用$+變數名來表示。識別符號在PHP中遵循下列選項中的那些規則?()...