首页 > 作文

php Memcache 中实现消息队列

更新时间:2023-04-06 14:07:29 阅读: 评论:0

对于一个很大的消息队列,频繁进行进行大数据库的序列化 和 反序列化,有太耗费。下面是我用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 __t($name, $value)

{

$this->add($name, $value);

return $this;

}

functiandroid4 0on impty()

{

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->t($this->real, $data);

$this->t(“real”, ($this->real + 1) % $this->maxsize);

return $this;

}

function dequeue()

{

if ($this->impty()) {

throw new exception(“queue is empty”);

}

$this->decrement(“size”);

$this->delete($this->front);

$this->t(“front”, ($this->front + 1) % $this->maxsize);

return $this;

}

function gettop()

{

return $this->get($this->front);

}

function getall()

{

return $this->getpage();

}

function getpage($offt = 0, $limit = 0)

{

if ($this->impty() || $this->size < $offt) {

return null;

}

$keys[] = $this->getkeybypos(($this->front + $offt) % $this->maxsize);

$num = 1;

for ($pos = ($this->front + $offt + 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->impty())

{

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 t($pos, $data)

{

$this->memcache->t($t十月革命的时间his->getkeybypos($pos), $data);

return $this;

}

private function get($pos)

{

return $this->memcache->get($this->getkeybypos($pos));

}

private function delete($pos)

{

r世界名著网eturn $this->memcache->delete($this->getkeybypos($pos));

}

private function getkeybypos($pos)

{

return $this->prefix . $this->name . $pos;

}

}

本文发布于:2023-04-06 14:07:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/f12a618b48ba553c7806fddaa4e6efcd.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php Memcache 中实现消息队列.doc

本文 PDF 下载地址:php Memcache 中实现消息队列.pdf

下一篇:返回列表
标签:队列   消息   操作   尾部
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图