redis getone getcount getlist update

23 篇文章 0 订阅
   $where = ['comment_id'=>$id,'pid'=>['neq',0],'status'=>1];
            $return = $this->where($where)->select();


  /**
     * 获取产品信息
     * @param $id
     * @param array $fieldArray
     * @return mixed
     */
   public function getOne($id,$fieldArray=array()){
        $key = $this->cacheHashKey[0].$id;
        if($this->redis && $this->redis->exists($key)){
            if(empty($fieldArray)){
                $rst = $this->redis->hGetAll($key);
            }else{
                $rst = $this->redis->hMGet($key,$fieldArray);
            }
        }else{
            $rst = $this->model->where(['id'=>$id])->find();
            if($rst){
                $this->redis->hMset($key,$rst);
            }
        }
        if(empty($rst)) return nil;        
        return $rst;
    }

 /**
     * 得到产品下交易个数
     * @param $uid
     * @param $type  产品类型  1兑换
     * @return mixed
     * *抽奖和兑换记录合并
     */
    public function getCount($type,$uid){
        $key = $this->cacheZsetUidKey[0].'1'.'_'.$uid;
        if( $this->redis && ($this->redis->exists($key)) ){
            $count = $this->redis->zCount($key,"-inf","+inf");
        }else{
            $sqlWhere ='uid='.$uid.' and status=1';
            $count =   $this->model->where($sqlWhere)->count();
        }
        return $count;
    }



/**
     * 获取产品 下某个用户的交易成功的id
     * 抽奖和兑换记录合并
     * @param $type
     * @param $uid
     * @param int $start
     * @param $end
     * @return mixed
     */
    public function getList($type,$uid,$start=0,$end=-1){
        $key = $this->cacheZsetUidKey[0].'1'.'_'.$uid;
        if( $this->redis && ($this->redis->exists($key)) ){
            $return = $this->redis->zRevRange($key,$start,$end);
        }else{
            $sqlWhere ='uid='.$uid.' AND ((TYPE=1 AND STATUS=1) OR (TYPE=2 AND STATUS=2))';
            $result = $this->model->where($sqlWhere)->order('id desc')->select();
            if($result && $this->redis){
                foreach($result as $k=>$v){
                    $this->redis->zAdd($key,$v['id'],$v['id']);
                }
            }else{
                $result=array();
            }
            if($end == -1) $end=count($result);
            $returnArray = array_slice($result,$start,$end-$start+1);
            foreach($returnArray as $k=>$v){
                $return[$k]=$v['id'];
            }
        }
        return $return;
    }


 /*
     * 更新
     */
    public function update($id,$data){
        $where = ['id'=>$id];
        $result = $this->where($where)->save($data);
        if($result === false){
            return false;
        }else{
            $key = $this->cacheKey[0].$id;
            if($this->redis && $this->redis->exists($key)){
                $this->redis->hMset($key,$data);               
            }
            return true;
        }
    }



   //修改产品状态(状态0未上线 1上线 2下线)20150828
    public function offline($idArray){
        if( !is_array($idArray))return false;
        $conditon['id']=array('in',$idArray);
        $conditon['status']=1;
        $rst= $this->model->where($conditon)->save(array('status'=>2));
        if($rst!==false && $rst>0){
            foreach($idArray as $id){
                $hkey = $this->cacheHashKey[0].$id;
                $zkey = $this->cacheZsetKey[0];
                if($this->redis){
                    $this->redis->zDelete($zkey,$id);
                    $this->redis->DEL($hkey);
                }
            }
            return true;
        }
        return false;
    } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值