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);
     }
     

   
 
}

 

转载于:https://www.cnblogs.com/open-i/p/6726966.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值