天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

對于一個很大的消息隊列,頻繁進行進行大數(shù)據(jù)庫的序列化 和 反序列化,有太耗費。下面是我用php 實現(xiàn)的一個消息隊列,只需要在尾部插入一個數(shù)據(jù),就操作尾部,不用操作整個消息隊列進行讀取,與操作。但是,這個消息隊列不是線程安全的,我只是盡量的避免了沖突的可能性。如果消息不是非常的密集,比如幾秒鐘才一個,還是可以考慮這樣使用的。
如果你要實現(xià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技術(shù)php Memcache 中實現(xiàn)消息隊列,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产69精品久久久久无码麻豆 | 蜜芽一二三区 | 狠狠色综合7777久夜色撩人 | 日欧一片内射VA在线影院 | 青柠在线观看视频在线 | 99视频在线免费 | 美国兽皇zoo在线播放 | 战狼4在线观看完免费完整版 | 国产线精品视频在线观看 | 18 japanese宾馆直播 | 99E久热只有精品8在线直播 | 国产精品亚洲一区二区三区久久 | 小色哥影院 | 97久久超碰中文字幕 | 午夜精品久久久久久久爽牛战 | 日本午夜福利无码高清 | 91av影院| 国产精品人妻无码久久久2022 | 国产午夜免费不卡精品理论片 | 成人免费一级毛片在线播放视频 | 麻豆官网入口 | 日日AV夜夜添久久奶无码 | 一抽一出BGM免费50分动漫 | 亚洲VA天堂VA欧美VA在线 | 最近日本MV字幕免费观看视频 | 一个人HD在线观看免费高清视频 | 国产成人一区二区三中文 | 中文国产乱码在线人妻一区二区 | 久久精品国产欧美 | 人妻免费久久久久久久了 | 美女被C污黄网站免费观看 美女白虎穴 | 国产精品免费大片一区二区 | 动漫女生的逼 | 巨胸美女狂喷奶水www网麻豆 | 国内精品视频一区二区在线观看 | 国产短视频精品区 | 亚洲精品国产精麻豆久久99 | 美国色情三级欧美三级纸匠情挑 | 老奶奶50p| 亚洲日本欧美天堂在线 | 男人和女人全黄一级毛片 |