- 相關(guān)推薦
分析PHP隊列是什么
PHP具有非常強大的功能,所有的CGI的功能PHP都能實(shí)現,而且支持幾乎所有流行的數據庫以及操作系統。最重要的是PHP可以用C、C++進(jìn)行程序的擴展!今天,小編為大家搜索整理了PHP隊列是什么,希望大家能有所收獲,更多精彩內容請持續關(guān)注我們考試網(wǎng)!
什么是隊列,是先進(jìn)先出的線(xiàn)性表,在具體應用中通常用鏈表或者數組來(lái)實(shí)現,隊列只允許在后端進(jìn)行插入操作,在前端進(jìn)行刪除操作。
什么情況下會(huì )用了隊列呢,并發(fā)請求又要保證事務(wù)的完整性的時(shí)候就會(huì )用到隊列,當然不排除使用其它更好的方法,知道的不仿說(shuō)說(shuō)看。
隊列還可以用于減輕數據庫服務(wù)器壓力,我們可以將不是即時(shí)數據放入到隊列中,在數據庫空閑的時(shí)候或者間隔一段時(shí)間后執行。比如訪(fǎng)問(wèn)計數器,沒(méi)有必要即時(shí)的執行訪(fǎng)問(wèn)增加的Sql,在沒(méi)有使用隊列的時(shí)候sql語(yǔ)句是這樣的,假設有5個(gè)人訪(fǎng)問(wèn):
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請求次數,從而達到減輕服務(wù)器壓力的效果, 當然訪(fǎng)問(wèn)量不是很大網(wǎng)站根本沒(méi)有這個(gè)必要。
下面一個(gè)隊列類(lèi):
/**
* 隊列
*
* @author jaclon
*
*/
class Queue
{
private $_queue = array();
protected $cache = null;
protected $queuecachename;
/**
* 構造方法
* @param string $queuename 隊列名稱(chēng)
*/
function __construct($queuename)
{
$this->cache =& Cache::instance();
$this->queuecachename = queue_ . $queuename;
$result = $this->cache->get($this->queuecachename);
if (is_array($result)) {
$this->_queue = $result;
}
}
/**
* 將一個(gè)單元單元放入隊列末尾
* @param mixed $value
*/
function enQueue($value)
{
$this->_queue[] = $value;
$this->cache->set($this->queuecachename, $this->_queue);
return $this;
}
/**
* 將隊列開(kāi)頭的一個(gè)或多個(gè)單元移出
* @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;
}
/**
* 將隊列開(kāi)頭的單元移出隊列
*/
function deQueue()
{
$entry = array_shift($this->_queue);
$this->cache->set($this->queuecachename, $this->_queue);
return $entry;
}
/**
* 返回隊列長(cháng)度
*/
function size()
{
return count($this->_queue);
}
/**
* 返回隊列中的第一個(gè)單元
*/
function peek()
{
return $this->_queue[0];
}
/**
* 返回隊列中的一個(gè)或多個(gè)單元
* @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隊列是什么】相關(guān)文章:
PHP隊列是什么10-29
php語(yǔ)言redis隊列操作實(shí)例08-19
php是什么11-13
PHP遞歸效率分析08-25
PHP與ASP的分析對比10-27
PHP 死鎖問(wèn)題分析05-19