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

php Memcache 中實現(xiàn)消息隊列

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

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;
}
}

相關文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    PHP類的使用 實例代碼講解

    用戶定義的類,也是學好 PHP 所必備的條件之一。而 PHP 的類,和其它的面向對象語言比較起來,還算蠻單純的。
    2009-12-12
  • php中神奇的fastcgi_finish_request

    php中神奇的fastcgi_finish_request

    當PHP運行在FastCGI模式時,PHP FPM提供了一個名為fastcgi_finish_request的方法。按照文檔上的說法,此方法可以提高請求的處理速度,如果有些處理可以在頁面生成完后再進行,就可以使用這個方法。
    2011-05-05
  • PHP安全配置詳細說明

    PHP安全配置詳細說明

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

    php使用strtotime技巧示例解惑

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

最新評論