php+redis队列链表操作类

php redis队列处理

/**
 * 队列链表操作类
 * 
 */
class QueueModel extends BaseModel
{


  public $redisHandler; //redis 句柄 
  const QUEUE_LIST_KEY='article_list_queue'; //队列保存键名
  const QUEUE_LIST_LEN='article_list_length';//队列长度保存键名
  const QUEUE_LIST_LEN_CACHE_TIME=3600; //队列长度保存键名缓存时间
    /**
     * 初始化
     */
    function __construct()
    {
        parent::__construct();
        //设置redis链接
        $this->redisHandler=$this->redis;
         
    }

    
    /**
     * [getLength 获取队列长度]
     * @return [type] [description]
     */
    public function getLength(){
           $length=(int)$this->redisHandler->get(self::QUEUE_LIST_LEN); 
           if(!empty($length)){
             $length=$this->redisHandler->lSize(self::QUEUE_LIST_KEY);
             $this->redisHandler->setex(self::QUEUE_LIST_LEN, self::QUEUE_LIST_LEN_CACHE_TIME, $length);
           }  
           return $length ? $length :0;
    }
   
    
    /**
     * [insertQueue 插入队列]
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function  insertQueue($data){
         $length=$this->redisHandler->lPush(self::QUEUE_LIST_KEY,serialize($data));
         if($length){
            $this->setInc(1);
         }
         return $length;
    }

     

     /**
      *[removeQueue 从队列中取出数据]
      * @return [type] [description]
      */
     public function removeQueue(){
            $value=$this->redisHandler->rPop(self::QUEUE_LIST_KEY);
            if($value){
                $this->setDec(1);
                return unserialize($value);
            }else{
                return false;
            }

         
     } 
     /**
      * [setInc 队列长度增加]
      * @param integer $value [description]
      */
     public function setInc($value=1){ 
        $list_len=$this->getLength()+$value;  
        return $this->redisHandler->setex(self::QUEUE_LIST_LEN, self::QUEUE_LIST_LEN_CACHE_TIME,  $list_len);
     }

     /**
      * [setDec 队列长度减少]
      * @param integer $value [description]
      */
     public function setDec($value=1){
         $list_len=$this->getLength()-$value; 
         return $this->redisHandler->setex(self::QUEUE_LIST_LEN, self::QUEUE_LIST_LEN_CACHE_TIME,  $list_len);
     }
     

   
 
}

 

posted @ 2017-04-18 11:39  茶觉  阅读(1098)  评论(0编辑  收藏  举报