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

更新时间:2016-02-19 11:04:57 点击次数:1962次
本文实例讲述了PHP实现的memcache环形队列类。分享给大家供大家参考。具体如下:

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

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

本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!