php缓存Memcache的Queue的用法

分享一篇关于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. //开源代码phpfensi.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);//取出一定数量的数据