国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php緩存Memcache的Queue的用法

2024-09-04 11:44:14
字體:
來源:轉載
供稿:網友

分享一篇關于php中緩存Memcache的Queue的用法,Memcache是一種緩存技術,可以提升程序的性能減輕服務器的壓力,代碼如下:

  1. <?php  
  2. class MQ{  
  3. public static $client;  
  4. private static $m_real;  
  5. private static $m_front;  
  6. private static $m_data = array();  
  7. const QUEUE_MAX_NUM = 100000000;  
  8. const QUEUE_FRONT_KEY = '_queue_item_front';  
  9. const QUEUE_REAL_KEY = '_queue_item_real';  
  10. public static function setupMq($conf) {  
  11. self::$client = memcache_pconnect($conf);  
  12. self::$m_real = memcache_get(self::$client, self::QUEUE_REAL_KEY);  
  13. self::$m_front = memcache_get(self::$client, self::QUEUE_FRONT_KEY);  
  14. if (!isset(self::$m_real) || emptyempty(self::$m_real)) {  
  15. self::$real= 0;  
  16. }  
  17. if (!isset(self::$m_front) || emptyempty(self::$m_front)) {  
  18. self::$m_front = 0;  
  19. }  
  20. return self::$client;  
  21. }  
  22. public static function add($queue$data) {  
  23. $result = false;  
  24. if (self::$m_real < self::QUEUE_MAX_NUM) {  
  25. if (memcache_add(self::$client$queue.self::$m_real$data)) {  
  26. self::mqRealChange();  
  27. $result = true;  
  28. }  
  29. }  
  30. return $result;  
  31. }  
  32. public static function get($key$count) {  
  33. $num = 0;  
  34. for ($i=self::$m_front;$i<self::$m_front + $count;$i++) {  
  35. if ($dataTmp = memcache_get(self::$client$key.$i)) {  
  36. self::$m_data[] = $dataTmp;  
  37. memcache_delete(self::$client$key.$i);  
  38. $num++;  
  39. }  
  40. }  
  41. if ($num>0) {  
  42. self::mqFrontChange($num);  
  43. }  
  44. return self::$m_data;  
  45. }  
  46. private static function mqRealChange() {  
  47. memcache_add(self::$client, self::QUEUE_REAL_KEY, 0);  
  48. self::$m_real = memcache_increment(self::$client, self::QUEUE_REAL_KEY, 1);  
  49. }  
  50. private static function mqFrontChange($num) {  
  51. memcache_add(self::$client, self::QUEUE_FRONT_KEY, 0);  
  52. self::$m_front = memcache_increment(self::$client, self::QUEUE_FRONT_KEY, $num);  
  53. }  
  54. public static function mflush($memcache_obj) {  
  55. memcache_flush($memcache_obj);  
  56. }  
  57. public static function Debug() {  
  58. echo 'real:'.self::$m_real."<br>/r/n";  
  59. echo 'front:'.self::$m_front."<br>/r/n";  
  60. echo 'wait for process data:'.intval(self::$m_real - self::$m_front);  
  61. echo "<br>/r/n";  
  62. echo '<pre>';  
  63. print_r(self::$m_data);  
  64. echo '<pre>';  
  65. }  
  66. }  
  67. define('FLUSH_MQ',0);//CLEAN ALL DATA  
  68. define('IS_ADD',0);//SET DATA  
  69. //開源代碼Vevb.com 
  70. $mobj = MQ::setupMq('127.0.0.1','11211');  
  71. if (FLUSH_MQ) {  
  72. MQ::mflush($mobj);  
  73. else {  
  74. if (IS_ADD) {  
  75. MQ::add('user_sync''1test');  
  76. MQ::add('user_sync''2test');  
  77. MQ::add('user_sync''3test');  
  78. MQ::add('user_sync''4test');  
  79. MQ::add('user_sync''5test');  
  80. MQ::add('user_sync''6test');  
  81. else {  
  82. MQ::get('user_sync', 10);  
  83. }  
  84. }  
  85. MQ::Debug();  
  86. ?> 

好了下面來看看用法,代碼如下:

  1. MQ::setupMq('127.0.0.1','11211');//連接  
  2. MQ::add($key$value);//添加數據到隊列  
  3. MQ::add($key$value);//添加數據到隊列  
  4. MQ::add($key$value);//添加數據到隊列  
  5. MQ::add($key$value);//添加數據到隊列  
  6. MQ::add($key$value);//添加數據到隊列  
  7. MQ::add($key$value);//添加數據到隊列  
  8. MQ:get($key, 10);//取出一定數量的數據

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 六安市| 麻阳| 新竹县| 肥乡县| 龙岩市| 兴安盟| 门头沟区| 永福县| 白水县| 普兰店市| 银川市| 关岭| 吕梁市| 湟中县| 施甸县| 呼图壁县| 阳江市| 辰溪县| 邵武市| 疏附县| 泰和县| 五家渠市| 渝中区| 宝鸡市| 含山县| 江陵县| 广州市| 伽师县| 海晏县| 淮滨县| 广元市| 张掖市| 衡水市| 宝兴县| 汨罗市| 顺义区| 巴林右旗| 宣城市| 太湖县| 龙海市| 彭州市|