搜索
首页后端开发php教程PHP memcache 环形队列

PHP memcache 环形队列类。新手,没咋学过数据结构,因为业务需要,所以只是硬着头皮模拟的! 原形是 oschina上 lusi 分享的PHP memcache 队列代码。为使队列随时可入可出,且不受int长度越界危险(单链采取Head自增的话不作处理有越界可能),所以索性改写成环形队列。可能还有BUG,忘见谅!
  1. /**
  2. * PHP memcache 环形队列类
  3. * 原作者 LKK/lianq.net
  4. * 修改 FoxHunter
  5. * 因业务需要只保留的队列中的Pop和Push,修改过期时间为0即永久
  6. */
  7. class MQueue
  8. {
  9. public static $client;
  10. private $expire; //过期时间,秒,1~2592000,即30天内
  11. private $sleepTime; //等待解锁时间,微秒
  12. private $queueName; //队列名称,唯一值
  13. private $retryNum; //尝试次数
  14. private $MAXNUM; //最大队列容量
  15. private $canRewrite; //是否可以覆写开关,满出来的内容从头部开始覆盖重写原来的数据
  16. private $HEAD; //下一步要进入的指针位置
  17. private $TAIL; //下一步要进入的指针位置
  18. private $LEN; //队列现有长度
  19. const LOCK_KEY = '_Fox_MQ_LOCK_'; //锁存储标示
  20. const LENGTH_KEY = '_Fox_MQ_LENGTH_'; //队列现长度存储标示
  21. const VALU_KEY = '_Fox_MQ_VAL_'; //队列键值存储标示
  22. const HEAD_KEY = '_Fox_MQ_HEAD_'; //队列HEAD指针位置标示
  23. const TAIL_KEY = '_Fox_MQ_TAIL_'; //队列TAIL指针位置标示
  24. /*
  25. * 构造函数
  26. * 对于同一个$queueName,实例化时必须保障构造函数的参数值一致,否则pop和push会导队列顺序混乱
  27. */
  28. public function __construct($queueName = '', $maxqueue = 1, $canRewrite = false, $expire = 0, $config = '')
  29. {
  30. if (empty($config)) {
  31. self::$client = memcache_pconnect('127.0.0.1', 11211);
  32. } elseif (is_array($config)) { //array('host'=>'127.0.0.1','port'=>'11211')
  33. self::$client = memcache_pconnect($config['host'], $config['port']);
  34. } elseif (is_string($config)) { //"127.0.0.1:11211"
  35. $tmp = explode(':', $config);
  36. $conf['host'] = isset($tmp[0]) ? $tmp[0] : '127.0.0.1';
  37. $conf['port'] = isset($tmp[1]) ? $tmp[1] : '11211';
  38. self::$client = memcache_pconnect($conf['host'], $conf['port']);
  39. }
  40. if (!self::$client)
  41. return false;
  42. ignore_user_abort(true); //当客户断开连接,允许继续执行
  43. set_time_limit(0); //取消脚本执行延时上限
  44. $this->access = false;
  45. $this->sleepTime = 1000;
  46. $expire = (empty($expire)) ? 0 : (int) $expire + 1;
  47. $this->expire = $expire;
  48. $this->queueName = $queueName;
  49. $this->retryNum = 20000;
  50. $this->MAXNUM = $maxqueue != null ? $maxqueue : 1;
  51. $this->canRewrite = $canRewrite;
  52. $this->getHeadAndTail();
  53. if (!isset($this->HEAD) || empty($this->HEAD))
  54. $this->HEAD = 0;
  55. if (!isset($this->TAIL) || empty($this->TAIL))
  56. $this->TAIL = 0;
  57. if (!isset($this->LEN) || empty($this->LEN))
  58. $this->LEN = 0;
  59. }
  60. //获取队列首尾指针信息和长度
  61. private function getHeadAndTail()
  62. {
  63. $this->HEAD = (int) memcache_get(self::$client, $this->queueName . self::HEAD_KEY);
  64. $this->TAIL = (int) memcache_get(self::$client, $this->queueName . self::TAIL_KEY);
  65. $this->LEN = (int) memcache_get(self::$client, $this->queueName . self::LENGTH_KEY);
  66. }
  67. // 利用memcache_add原子性加锁
  68. private function lock()
  69. {
  70. if ($this->access === false) {
  71. $i = 0;
  72. while (!memcache_add(self::$client, $this->queueName . self::LOCK_KEY, 1, false, $this->expire)) {
  73. usleep($this->sleepTime);
  74. @$i++;
  75. if ($i > $this->retryNum) { //尝试等待N次
  76. return false;
  77. break;
  78. }
  79. }
  80. return $this->access = true;
  81. }
  82. return false;
  83. }
  84. //更新头部指针指向,指向下一个位置
  85. private function incrHead()
  86. {
  87. //$this->getHeadAndTail(); //获取最新指针信息 ,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释
  88. $this->HEAD++; //头部指针下移
  89. if ($this->HEAD >= $this->MAXNUM) {
  90. $this->HEAD = 0; //边界值修正
  91. }
  92. ;
  93. $this->LEN--; //Head的移动由Pop触发,所以相当于数量减少
  94. if ($this->LEN $this->LEN = 0; //边界值修正
  95. }
  96. ;
  97. memcache_set(self::$client, $this->queueName . self::HEAD_KEY, $this->HEAD, false, $this->expire); //更新
  98. memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
  99. }
  100. //更新尾部指针指向,指向下一个位置
  101. private function incrTail()
  102. {
  103. //$this->getHeadAndTail(); //获取最新指针信息,由于本方法体均在锁内调用,其锁内已调用了此方法,本行注释
  104. $this->TAIL++; //尾部指针下移
  105. if ($this->TAIL >= $this->MAXNUM) {
  106. $this->TAIL = 0; //边界值修正
  107. }
  108. ;
  109. $this->LEN++; //Head的移动由Push触发,所以相当于数量增加
  110. if ($this->LEN >= $this->MAXNUM) {
  111. $this->LEN = $this->MAXNUM; //边界值长度修正
  112. }
  113. ;
  114. memcache_set(self::$client, $this->queueName . self::TAIL_KEY, $this->TAIL, false, $this->expire); //更新
  115. memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
  116. }
  117. // 解锁
  118. private function unLock()
  119. {
  120. memcache_delete(self::$client, $this->queueName . self::LOCK_KEY);
  121. $this->access = false;
  122. }
  123. //判断是否满队列
  124. public function isFull()
  125. {
  126. //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  127. if ($this->canRewrite)
  128. return false;
  129. return $this->LEN == $this->MAXNUM ? true : false;
  130. }
  131. //判断是否为空
  132. public function isEmpty()
  133. {
  134. //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  135. return $this->LEN == 0 ? true : false;
  136. }
  137. public function getLen()
  138. {
  139. //外部直接调用的时候由于没有锁所以此处的值是个大概值,并不很准确,但是内部调用由于在前面有lock,所以可信
  140. return $this->LEN;
  141. }
  142. /*
  143. * push值
  144. * @param mixed 值
  145. * @return bool
  146. */
  147. public function push($data = '')
  148. {
  149. $result = false;
  150. if (empty($data))
  151. return $result;
  152. if (!$this->lock()) {
  153. return $result;
  154. }
  155. $this->getHeadAndTail(); //获取最新指针信息
  156. if ($this->isFull()) { //只有在非覆写下才有Full概念
  157. $this->unLock();
  158. return false;
  159. }
  160. if (memcache_set(self::$client, $this->queueName . self::VALU_KEY . $this->TAIL, $data, MEMCACHE_COMPRESSED, $this->expire)) {
  161. //当推送后,发现尾部和头部重合(此时指针还未移动),且右边仍有未由Head读取的数据,那么移动Head指针,避免尾部指针跨越Head
  162. if ($this->TAIL == $this->HEAD && $this->LEN >= 1) {
  163. $this->incrHead();
  164. }
  165. $this->incrTail(); //移动尾部指针
  166. $result = true;
  167. }
  168. $this->unLock();
  169. return $result;
  170. }
  171. /*
  172. * Pop一个值
  173. * @param [length] int 队列长度
  174. * @return array
  175. */
  176. public function pop($length = 0)
  177. {
  178. if (!is_numeric($length))
  179. return false;
  180. if (!$this->lock())
  181. return false;
  182. $this->getHeadAndTail();
  183. if (empty($length))
  184. $length = $this->LEN; //默认读取所有
  185. if ($this->isEmpty()) {
  186. $this->unLock();
  187. return false;
  188. }
  189. //获取长度超出队列长度后进行修正
  190. if ($length > $this->LEN)
  191. $length = $this->LEN;
  192. $data = $this->popKeyArray($length);
  193. $this->unLock();
  194. return $data;
  195. }
  196. /*
  197. * pop某段长度的值
  198. * @param [length] int 队列长度
  199. * @return array
  200. */
  201. private function popKeyArray($length)
  202. {
  203. $result = array();
  204. if (empty($length))
  205. return $result;
  206. for ($k = 0; $k $result[] = @memcache_get(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD);
  207. @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $this->HEAD, 0);
  208. //当提取值后,发现头部和尾部重合(此时指针还未移动),且右边没有数据,即队列中最后一个数据被完全掏空,此时指针停留在本地不移动,队列长度变为0
  209. if ($this->TAIL == $this->HEAD && $this->LEN $this->LEN = 0;
  210. memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, $this->LEN, false, $this->expire); //更新
  211. break;
  212. } else {
  213. $this->incrHead(); //首尾未重合,或者重合但是仍有未读取出的数据,均移动HEAD指针到下一处待读取位置
  214. }
  215. }
  216. return $result;
  217. }
  218. /*
  219. * 重置队列
  220. * * @return NULL
  221. */
  222. private function reset($all = false)
  223. {
  224. if ($all) {
  225. memcache_delete(self::$client, $this->queueName . self::HEAD_KEY, 0);
  226. memcache_delete(self::$client, $this->queueName . self::TAIL_KEY, 0);
  227. memcache_delete(self::$client, $this->queueName . self::LENGTH_KEY, 0);
  228. } else {
  229. $this->HEAD = $this->TAIL = $this->LEN = 0;
  230. memcache_set(self::$client, $this->queueName . self::HEAD_KEY, 0, false, $this->expire);
  231. memcache_set(self::$client, $this->queueName . self::TAIL_KEY, 0, false, $this->expire);
  232. memcache_set(self::$client, $this->queueName . self::LENGTH_KEY, 0, false, $this->expire);
  233. }
  234. }
  235. /*
  236. * 清除所有memcache缓存数据
  237. * @return NULL
  238. */
  239. public function memFlush()
  240. {
  241. memcache_flush(self::$client);
  242. }
  243. public function clear($all = false)
  244. {
  245. if (!$this->lock())
  246. return false;
  247. $this->getHeadAndTail();
  248. $Head = $this->HEAD;
  249. $Length = $this->LEN;
  250. $curr = 0;
  251. for ($i = 0; $i $curr = $this->$Head + $i;
  252. if ($curr >= $this->MAXNUM) {
  253. $this->HEAD = $curr = 0;
  254. }
  255. @memcache_delete(self::$client, $this->queueName . self::VALU_KEY . $curr, 0);
  256. }
  257. $this->unLock();
  258. $this->reset($all);
  259. return true;
  260. }
  261. }
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
可以在PHP会话中存储哪些数据?可以在PHP会话中存储哪些数据?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,数字,数组和原始物。

您如何开始PHP会话?您如何开始PHP会话?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考虑使用AttActAcks.s.s.4)

什么是会话再生,如何提高安全性?什么是会话再生,如何提高安全性?May 02, 2025 am 12:15 AM

会话再生是指在用户进行敏感操作时生成新会话ID并使旧ID失效,以防会话固定攻击。实现步骤包括:1.检测敏感操作,2.生成新会话ID,3.销毁旧会话ID,4.更新用户端会话信息。

使用PHP会话时有哪些性能考虑?使用PHP会话时有哪些性能考虑?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显着影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP会话与Cookie有何不同?PHP会话与Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何识别用户的会话?PHP如何识别用户的会话?May 01, 2025 am 12:23 AM

phpientifiesauser'ssessionusessessionSessionCookiesAndSessionIds.1)whiwSession_start()被称为,phpgeneratesainiquesesesessionIdStoredInacookInAcookInamedInAcienamedphpsessidontheuser'sbrowser'sbrowser.2)thisIdAllowSphptptpptpptpptpptortoreTessessionDataAfromtheserverMtheserver。

确保PHP会议的一些最佳实践是什么?确保PHP会议的一些最佳实践是什么?May 01, 2025 am 12:22 AM

PHP会话的安全可以通过以下措施实现:1.使用session_regenerate_id()在用户登录或重要操作时重新生成会话ID。2.通过HTTPS协议加密传输会话ID。3.使用session_save_path()指定安全目录存储会话数据,并正确设置权限。

PHP会话文件默认存储在哪里?PHP会话文件默认存储在哪里?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。