微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP实现的memcache环形队列类实例

本文实例讲述了PHP实现的memcache环形队列类。分享给大家供大家参考。具体如下:

这里介绍了PHP实现的memcache环形队列类。没咋学过数据结构,因为业务需要,所以只是硬着头皮模拟的! 参考PHP memcache 队列代码。为使队列随时可入可出,且不受int长度越界危险(单链采取Head自增的话不作处理有越界可能),所以索性改写成环形队列。可能还有BUG,忘见谅!

rush:PHP;"> '127.0.0.1','port'=>'11211') self::$client = memcache_pconnect($config['host'],$config['port']); } elseif (is_string($config)) { //"127.0.0.1:11211" $tmp = explode(':',$config); $conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1'; $conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211'; self::$client = memcache_pconnect($conf['host'],$conf['port']); } if (!self::$client) return false; ignore_user_abort(true); //当客户断开连接,允许继续执行 set_time_limit(0); //取消脚本执行延时上限 $this->access = false; $this->sleepTime = 1000; $expire = (empty($expire)) ? 0 : (int) $expire + 1; $this->expire = $expire; $this->queueName = $queueName; $this->retryNum = 20000; $this->MAxnuM = $maxqueue != null ? $maxqueue : 1; $this->canRewrite = $canRewrite; $this->getHeadAndTail(); if (!isset($this->HEAD) || empty($this->HEAD)) $this->HEAD = 0; if (!isset($this->TAIL) || empty($this->TAIL)) $this->TAIL = 0; if (!isset($this->LEN) || empty($this->LEN)) $this->LEN = 0; } //获取队列首尾指针信息和长度 private function getHeadAndTail() { $this->HEAD = (int) memcache_get(self::$client,$this->queueName . self::HEAD_KEY); $this->TAIL = (int) memcache_get(self::$client,$this->queueName . self::TAIL_KEY); $this->LEN = (int) memcache_get(self::$client,$this->queueName . self::LENGTH_KEY); } // 利用memcache_add原子性加锁 private function lock() { if ($this->access === false) { $i = 0; while (!memcache_add(self::$client,$this->queueName . self::LOCK_KEY,1,false,$this->expire)) { usleep($this->sleepTime); @$i++; if ($i > $this->retryNum) { //尝试等待N次 return false; break; } } return $this->access = true; } return false; } //更新头部指针指向,指向下一个位置 private function incrHead() { //$this->getHeadAndTail(); //获取最新指针信息,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释 $this->HEAD++; //头部指针下移 if ($this->HEAD >= $this->MAxnuM) { $this->HEAD = 0; //边界值修正 } ; $this->LEN--; //Head的移动由Pop触发,所以相当于数量减少 if ($this->LEN < 0) { $this->LEN = 0; //边界值修正 } ; memcache_set(self::$client,$this->queueName . self::HEAD_KEY,$this->HEAD,$this->expire); //更新 memcache_set(self::$client,$this->queueName . self::LENGTH_KEY,$this->LEN,$this->expire); //更新 } //更新尾部指针指向,指向下一个位置 private function incrTail() { //$this->getHeadAndTail(); //获取最新指针信息,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释 $this->TAIL++; //尾部指针下移 if ($this->TAIL >= $this->MAxnuM) { $this->TAIL = 0; //边界值修正 } ; $this->LEN++; //Head的移动由Push触发,所以相当于数量增加 if ($this->LEN >= $this->MAxnuM) { $this->LEN = $this->MAxnuM; //边界值长度修正 } ; memcache_set(self::$client,$this->queueName . self::TAIL_KEY,$this->TAIL,$this->expire); //更新 } // 解锁 private function unLock() { memcache_delete(self::$client,$this->queueName . self::LOCK_KEY); $this->access = false; } //判断是否满队列 public function isFull() { //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信 if ($this->canRewrite) return false; return $this->LEN == $this->MAxnuM ? true : false; } //判断是否为空 public function isEmpty() { //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信 return $this->LEN == 0 ? true : false; } public function getLen() { //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信 return $this->LEN; } /* * push值 * @param mixed 值 * @return bool */ public function push($data = '') { $result = false; if (empty($data)) return $result; if (!$this->lock()) { return $result; } $this->getHeadAndTail(); //获取最新指针信息 if ($this->isFull()) { //只有在非覆写下才有Full概念 $this->unLock(); return false; } if (memcache_set(self::$client,$this->queueName . self::VALU_KEY . $this->TAIL,$data,MEMCACHE_COMpressed,$this->expire)) { //当推送后,发现尾部和头部重合(此时指针还未移动),且右边仍有未由Head读取的数据,那么移动Head指针,避免尾部指针跨越Head if ($this->TAIL == $this->HEAD && $this->LEN >= 1) { $this->incrHead(); } $this->incrTail(); //移动尾部指针 $result = true; } $this->unLock(); return $result; } /* * Pop一个值 * @param [length] int 队列长度 * @return array */ public function pop($length = 0) { if (!is_numeric($length)) return false; if (!$this->lock()) return false; $this->getHeadAndTail(); if (empty($length)) $length = $this->LEN; //认读取所有 if ($this->isEmpty()) { $this->unLock(); return false; } //获取长度超出队列长度后进行修正 if ($length > $this->LEN) $length = $this->LEN; $data = $this->popKeyArray($length); $this->unLock(); return $data; } /* * pop某段长度的值 * @param [length] int 队列长度 * @return array */ private function popKeyArray($length) { $result = array(); if (empty($length)) return $result; for ($k = 0; $k < $length; $k++) { $result[] = @memcache_get(self::$client,$this->queueName . self::VALU_KEY . $this->HEAD); @memcache_delete(self::$client,$this->queueName . self::VALU_KEY . $this->HEAD,0); //当提取值后,发现头部和尾部重合(此时指针还未移动),且右边没有数据,即队列中最后一个数据被完全掏空,此时指针停留在本地不移动,队列长度变为0 if ($this->TAIL == $this->HEAD && $this->LEN <= 1) { $this->LEN = 0; memcache_set(self::$client,$this->expire); //更新 break; } else { $this->incrHead(); //首尾未重合,或者重合但是仍有未读取出的数据,均移动HEAD指针到下一处待读取位置 } } return $result; } /* * 重置队列 * * @return NULL */ private function reset($all = false) { if ($all) { memcache_delete(self::$client,0); memcache_delete(self::$client,0); } else { $this->HEAD = $this->TAIL = $this->LEN = 0; memcache_set(self::$client,$this->expire); memcache_set(self::$client,$this->expire); } } /* * 清除所有memcache缓存数据 * @return NULL */ public function memFlush() { memcache_flush(self::$client); } public function clear($all = false) { if (!$this->lock()) return false; $this->getHeadAndTail(); $Head = $this->HEAD; $Length = $this->LEN; $curr = 0; for ($i = 0; $i < $Length; $i++) { $curr = $this->$Head + $i; if ($curr >= $this->MAxnuM) { $this->HEAD = $curr = 0; } @memcache_delete(self::$client,$this->queueName . self::VALU_KEY . $curr,0); } $this->unLock(); $this->reset($all); return true; } }

希望本文所述对大家的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中NATIVE的支付实现流程与PC端实现扫码支付流程
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中APP的支付的配置与实现流程
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户信息这个功能的开发流程。 配置 1.首先得在微信公众平台申请一下微信小程序账号并获取到小程序的AppID和AppSecret https://mp.weixin.qq.com/cgi-bin/loginpage?url=%2Fwxamp%2F
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一款开源且免费商用的后台开发框架,它基于ThinkPHP和Bootstrap两大主流技术构建的极速后台开发框架,它有着非常完善且强大的功能和便捷的开发体验,使我逐渐喜欢上了它。
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛,就离不开通讯了,然后我就想到了长连接。这里本人用的是GatewayWorker框架。
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中JSAPI的支付实现流程
服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用