php Memcache中實現消息隊列
對於一個很大的消息隊列,頻繁進行進行大數據庫的序列化 和 反序列化,有太耗費。下面是我用PHP 實現的一個消息隊列,只需要在尾部插入一個數據,就操作尾部,不用操作整個消息隊列進行讀取,與操作。但是,這個消息隊列不是線程安全的,我只是儘量的避免了衝突的`可能性。如果消息不是非常的密集,比如幾秒鐘才一個,還是可以考慮這樣使用的。就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!
如果你要實現線程安全的,一個建議是通過文件進行鎖定,然後進行操作。下面是代碼:
複製代碼 代碼如下:
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;
}
} </p
相關文章
-
PHP中的Memcache基礎知識
導語:memcache是一套分佈式的高速緩存系統,由LiveJoual的Brad Fitzpatrick開發,但目前被許多網站使用以提升網站的訪問速度,尤其對於一些大型的、需要頻繁訪問數據庫的網站訪問速度提升效果十分顯著。以下的是本站小編搜 -
怎麼用PHP安裝memcached擴展筆記
主要介紹了PHP安裝memcached擴展筆記,本文講解了安裝服務端、launchpad、安裝memcached擴展以及配置等內容,需要的朋友可以參考下。最近在服務器上部緩存系統,記錄一下PHP安裝memcached擴展。 複製代碼 代碼如下:# -
PHP內存緩存Memcached類實例
PHP內存緩存Memcached類,以實例形式分析了PHP內存緩存Memcached的實現方法,是php操作memcached的典型應用,非常具有實用價值,需要的朋友可以參考下。 具體實現方法如下:複製代碼 代碼如下:<?PHPclass MemcacheModel -
php-memcache安裝方法
導語:PHP Memcache是一款圖形界面的監控工具,下面是安裝方法,歡迎參考!本機環境:win7 64位 php 5.3.10(wamp)Memcache分爲服務端和客戶端兩個部分。服務端是Memcached。Memcached 是一個高性能的分佈式內存對象緩存系統,用於 -
如何啓用Apache的PHP-FPM多實例
總結是事後對某一時期、某一項目或某些工作進行回顧和分析,從而做出帶有規律性的結論,它可使零星的、膚淺的、表面的感性認知上升到全面的、系統的、本質的理性認識上來,讓我們一起認真地寫一份總結吧。那麼總結有什麼格 -
在windows系統上讓php支持memcached步驟教程
導語:2018年農村致富好項目都有哪些呢?以下是小編爲大家整理的文章,歡迎閱讀!希望對大家有所幫助!牆紙經營與室內裝修牆紙是現代家裝的一種材料,在城裏很多室內裝修都會用到它,如今也隨着科技的發展,牆紙的樣式也發生翻天 -
Compare with the domestic hen and the duck英語作文
The domestic hen and the duck both belong to the bird category. They have some features in common such as wings, feet, beak, flight, nest, migration, food and eggs.The domestic hen has short wings whi -
WinXP如何安裝Apache+MySQL+PHP環境
安裝步驟:在整個wamp環境搭建中,本質的工作如下:1,配置系統對php中dll文件能默認處於調用狀態。在windos下,對dll文件系統默認處於調用狀態的,有兩種採用的方式。第一種是:把需要調用dll文件複製到C:windows system32(如果是W -
php中namespace use用法例子分析
class把一個一個function組織起來,namespace可以理解成把一個一個class,function等有序的'組織起來。個人覺得,namespace的主要優勢有第一,可以更好的管理代碼第二,文件一多,可以避免class,function的重名第三,代碼可讀 -
如何用Photoshop CC中的Camera Raw來進行後期修圖
自從Photoshop CC中置入了Camea Raw後,我們就可以用Photoshop CC對各種格式的圖片進行後期處理,而不再僅限於Raw格式的圖片。事實上,我們可以將Camera Raw當做一個智能濾鏡來用,可以用來調整曝光、銳化、降噪、局部調整和