php Memcache 中實(shí)現(xiàn)消息隊(duì)列
更新時(shí)間:2009年11月24日 21:42:32 作者:
Memcache 一般用于緩存服務(wù)。但是很多時(shí)候,比如一個(gè)消息廣播系統(tǒng),需要一個(gè)消息隊(duì)列。直接從數(shù)據(jù)庫(kù)取消息,負(fù)載往往不行。如果將整個(gè)消息隊(duì)列用一個(gè)key緩存到memcache里面.
對(duì)于一個(gè)很大的消息隊(duì)列,頻繁進(jìn)行進(jìn)行大數(shù)據(jù)庫(kù)的序列化 和 反序列化,有太耗費(fèi)。下面是我用PHP 實(shí)現(xiàn)的一個(gè)消息隊(duì)列,只需要在尾部插入一個(gè)數(shù)據(jù),就操作尾部,不用操作整個(gè)消息隊(duì)列進(jìn)行讀取,與操作。但是,這個(gè)消息隊(duì)列不是線程安全的,我只是盡量的避免了沖突的可能性。如果消息不是非常的密集,比如幾秒鐘才一個(gè),還是可以考慮這樣使用的。
如果你要實(shí)現(xiàn)線程安全的,一個(gè)建議是通過(guò)文件進(jìn)行鎖定,然后進(jì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;
}
}
如果你要實(shí)現(xiàn)線程安全的,一個(gè)建議是通過(guò)文件進(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;
}
}
您可能感興趣的文章:
- PHP+memcache實(shí)現(xiàn)消息隊(duì)列案例分享
- php+redis消息隊(duì)列實(shí)現(xiàn)搶購(gòu)功能
- PHP消息隊(duì)列用法實(shí)例分析
- phpredis提高消息隊(duì)列的實(shí)時(shí)性方法(推薦)
- 使用PHP訪問(wèn)RabbitMQ消息隊(duì)列的方法示例
- PHP消息隊(duì)列實(shí)現(xiàn)及應(yīng)用詳解【隊(duì)列處理訂單系統(tǒng)和配送系統(tǒng)】
- PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列的方法詳解
- PHP多進(jìn)程通信-消息隊(duì)列使用
- PHP PDO和消息隊(duì)列的個(gè)人理解與應(yīng)用實(shí)例分析
- PHP高級(jí)編程之消息隊(duì)列原理與實(shí)現(xiàn)方法詳解
相關(guān)文章
php+resumablejs實(shí)現(xiàn)的分塊上傳 斷點(diǎn)續(xù)傳功能示例
這篇文章主要介紹了php+resumablejs實(shí)現(xiàn)的分塊上傳 斷點(diǎn)續(xù)傳功能,結(jié)合實(shí)例形式分析了php+resumablejs文件傳輸?shù)木唧w實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
php實(shí)現(xiàn)簡(jiǎn)單的MVC框架實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)簡(jiǎn)單的MVC框架,較為詳細(xì)的分析了php實(shí)現(xiàn)MVC框架的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
php實(shí)現(xiàn)從ftp服務(wù)器上下載文件樹(shù)到本地電腦的程序
功能:完成從ftp服務(wù)器端下載文件到本地電腦(整體復(fù)制即目錄樹(shù)是一樣的)2009-02-02
php判斷上傳的Excel文件中是否有圖片及PHPExcel庫(kù)認(rèn)識(shí)
php判斷Excel文件中是否有圖片,大家很想知道如何實(shí)現(xiàn)吧,不要走開(kāi)接下來(lái)為您揭曉,感興趣的朋友可以了解下哦2013-01-01
PHP入門(mén)教程之面向?qū)ο蠡靖拍顚?shí)例分析
這篇文章主要介紹了PHP入門(mén)教程之面向?qū)ο蠡靖拍?結(jié)合實(shí)例形式簡(jiǎn)單分析了php面向?qū)ο笏婕暗念惖亩x、對(duì)象的創(chuàng)建、構(gòu)造函數(shù)、成員變量、成員方法等,需要的朋友可以參考下2016-09-09

