0

0

Memcache 中实现消息队列_PHP教程

php中文网

php中文网

发布时间:2016-07-20 11:06:48

|

1318人浏览过

|

来源于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 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;
}
}


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445008.htmlTechArticleclass Memcache_Queue { private $memcache; private $name; private $prefix; function __construct($maxSize, $name, $memcache, $prefix = __memcache_queue__) { if ($memcache == null) {...

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
c++ 根号
c++ 根号

本专题整合了c++根号相关教程,阅读专题下面的文章了解更多详细内容。

22

2026.01.23

c++空格相关教程合集
c++空格相关教程合集

本专题整合了c++空格相关教程,阅读专题下面的文章了解更多详细内容。

24

2026.01.23

yy漫画官方登录入口地址合集
yy漫画官方登录入口地址合集

本专题整合了yy漫画入口相关合集,阅读专题下面的文章了解更多详细内容。

99

2026.01.23

漫蛙最新入口地址汇总2026
漫蛙最新入口地址汇总2026

本专题整合了漫蛙最新入口地址大全,阅读专题下面的文章了解更多详细内容。

132

2026.01.23

C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

15

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

65

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

61

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

63

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
memcache基础课程
memcache基础课程

共6课时 | 1.6万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号