欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php Memcache 中實(shí)現(xiàn)消息隊(duì)列

 更新時(shí)間:2009年11月24日 21:42:32   作者:  
Memcache 一般用于緩存服務(wù)。但是很多時(shí)候,比如一個(gè)消息廣播系統(tǒng),需要一個(gè)消息隊(duì)列。直接從數(shù)據(jù)庫取消息,負(fù)載往往不行。如果將整個(gè)消息隊(duì)列用一個(gè)key緩存到memcache里面.
對(duì)于一個(gè)很大的消息隊(duì)列,頻繁進(jìn)行進(jìn)行大數(shù)據(jù)庫的序列化 和 反序列化,有太耗費(fèi)。下面是我用PHP 實(shí)現(xiàn)的一個(gè)消息隊(duì)列,只需要在尾部插入一個(gè)數(shù)據(jù),就操作尾部,不用操作整個(gè)消息隊(duì)列進(jìn)行讀取,與操作。但是,這個(gè)消息隊(duì)列不是線程安全的,我只是盡量的避免了沖突的可能性。如果消息不是非常的密集,比如幾秒鐘才一個(gè),還是可以考慮這樣使用的。
如果你要實(shí)現(xiàn)線程安全的,一個(gè)建議是通過文件進(jìn)行鎖定,然后進(jìn)行操作。下面是代碼:
復(fù)制代碼 代碼如下:

class Memcache_Queue
{
private $memcache;
private $name;
private $prefix;
function __construct($maxSize, $name, $memcache, $prefix = "__memcache_queue__")
{
if ($memcache == null) {
throw new Exception("memcache object is null, new the object first.");
}
$this->memcache = $memcache;
$this->name = $name;
$this->prefix = $prefix;
$this->maxSize = $maxSize;
$this->front = 0;
$this->real = 0;
$this->size = 0;
}
function __get($name)
{
return $this->get($name);
}
function __set($name, $value)
{
$this->add($name, $value);
return $this;
}
function isEmpty()
{
return $this->size == 0;
}
function isFull()
{
return $this->size == $this->maxSize;
}
function enQueue($data)
{
if ($this->isFull()) {
throw new Exception("Queue is Full");
}
$this->increment("size");
$this->set($this->real, $data);
$this->set("real", ($this->real + 1) % $this->maxSize);
return $this;
}
function deQueue()
{
if ($this->isEmpty()) {
throw new Exception("Queue is Empty");
}
$this->decrement("size");
$this->delete($this->front);
$this->set("front", ($this->front + 1) % $this->maxSize);
return $this;
}
function getTop()
{
return $this->get($this->front);
}
function getAll()
{
return $this->getPage();
}
function getPage($offset = 0, $limit = 0)
{
if ($this->isEmpty() || $this->size < $offset) {
return null;
}
$keys[] = $this->getKeyByPos(($this->front + $offset) % $this->maxSize);
$num = 1;
for ($pos = ($this->front + $offset + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
$num++;
if ($limit > 0 && $limit == $num) {
break;
}
}
return array_values($this->memcache->get($keys));
}
function makeEmpty()
{
$keys = $this->getAllKeys();
foreach ($keys as $value) {
$this->delete($value);
}
$this->delete("real");
$this->delete("front");
$this->delete("size");
$this->delete("maxSize");
}
private function getAllKeys()
{
if ($this->isEmpty())
{
return array();
}
$keys[] = $this->getKeyByPos($this->front);
for ($pos = ($this->front + 1) % $this->maxSize; $pos != $this->real; $pos = ($pos + 1) % $this->maxSize)
{
$keys[] = $this->getKeyByPos($pos);
}
return $keys;
}
private function add($pos, $data)
{
$this->memcache->add($this->getKeyByPos($pos), $data);
return $this;
}
private function increment($pos)
{
return $this->memcache->increment($this->getKeyByPos($pos));
}
private function decrement($pos)
{
$this->memcache->decrement($this->getKeyByPos($pos));
}
private function set($pos, $data)
{
$this->memcache->set($this->getKeyByPos($pos), $data);
return $this;
}
private function get($pos)
{
return $this->memcache->get($this->getKeyByPos($pos));
}
private function delete($pos)
{
return $this->memcache->delete($this->getKeyByPos($pos));
}
private function getKeyByPos($pos)
{
return $this->prefix . $this->name . $pos;
}
}

相關(guān)文章

  • 使用XHGui來測試PHP性能的教程

    使用XHGui來測試PHP性能的教程

    這篇文章主要介紹了使用XHGui來測試PHP性能的教程,示例演示基于Ubuntu操作系統(tǒng),需要的朋友可以參考下
    2015-07-07
  • 采用PHP函數(shù)memory_get_usage獲取PHP內(nèi)存清耗量的方法

    采用PHP函數(shù)memory_get_usage獲取PHP內(nèi)存清耗量的方法

    PHP性能優(yōu)化過程中需要獲取PHP內(nèi)存消耗,使用memory_get_usage()函數(shù)可獲取當(dāng)前的內(nèi)存消耗情況,函數(shù)使用簡單,這里討論一下memory_get_usage()函數(shù)的用法與實(shí)例
    2011-12-12
  • php實(shí)現(xiàn)多維數(shù)組中每個(gè)單元值(數(shù)字)翻倍的方法

    php實(shí)現(xiàn)多維數(shù)組中每個(gè)單元值(數(shù)字)翻倍的方法

    這篇文章主要介紹了php實(shí)現(xiàn)多維數(shù)組中每個(gè)單元值(數(shù)字)翻倍的方法,涉及php操作數(shù)組的技巧,需要的朋友可以參考下
    2015-02-02
  • 詳解php中curl返回false的解決辦法

    詳解php中curl返回false的解決辦法

    這篇文章主要介紹了php中curl返回false的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系

    php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系

    php+mysql實(shí)現(xiàn)無限級(jí)分類 | 樹型顯示分類關(guān)系...
    2006-11-11
  • 在WordPress中使用PHP腳本來判斷訪客來自什么國家

    在WordPress中使用PHP腳本來判斷訪客來自什么國家

    這篇文章主要介紹了在WordPress中使用PHP腳本來判斷訪客來自什么國家的方法,同時(shí)也可以調(diào)用文中所介紹的國內(nèi)網(wǎng)站所提供的API來查詢,需要的朋友可以參考下
    2015-12-12
  • PHP類的使用 實(shí)例代碼講解

    PHP類的使用 實(shí)例代碼講解

    用戶定義的類,也是學(xué)好 PHP 所必備的條件之一。而 PHP 的類,和其它的面向?qū)ο笳Z言比較起來,還算蠻單純的。
    2009-12-12
  • php中神奇的fastcgi_finish_request

    php中神奇的fastcgi_finish_request

    當(dāng)PHP運(yùn)行在FastCGI模式時(shí),PHP FPM提供了一個(gè)名為fastcgi_finish_request的方法。按照文檔上的說法,此方法可以提高請(qǐng)求的處理速度,如果有些處理可以在頁面生成完后再進(jìn)行,就可以使用這個(gè)方法。
    2011-05-05
  • PHP安全配置詳細(xì)說明

    PHP安全配置詳細(xì)說明

    PHP勿庸置疑是非常強(qiáng)大的服務(wù)器端腳本語言,但是強(qiáng)大的功能總是伴隨著重大的危險(xiǎn),在這章里,你將學(xué)習(xí)到使用PHP的安全模式來阻止一些PHP潛在的危險(xiǎn)因素。
    2011-09-09
  • php使用strtotime技巧示例解惑

    php使用strtotime技巧示例解惑

    這篇文章主要為大家介紹了php使用strtotime技巧示例解惑,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評(píng)論