PHP处理大数据量老用户头像更新的操作--解决数据量大超时的问题

    /**
     * @title 老用户头像更新--每3秒调用一次接口,每次更新10条数据
     * @example user/createHeadPicForOldUser?
     * @method GET
     * @author 邹柯
     */
    public function createHeadPicForOldUserAction(){
        $domain=$_SERVER['HTTP_HOST'];
        $ob = new UserModel();
        $user=M('user');
        $u_where="head_pic is null or head_pic=''";
        $count=$user->where($u_where)->count();
        $user_info=$user->field('user_id')->where($u_where)->page(1,10)->select();
        if($count !=0){
            for($i=0;$i<10;$i++){
                //获取一个默认头像
                $h_pic=$ob->getDefaultUserHeadPic();
                $re=$user->data(['head_pic'=>$h_pic])->where(['user_id'=>$user_info[$i]['user_id']])->save();
            }
            echo "还剩".$count."条记录没更新";
            exit("<script>setTimeout(function(){location.href='http://$domain/user/createHeadPicForOldUser';},3000);</script>");
        }else{
            exit("更新完毕");
        }        
        
    }
    //注册时给用户分配一个随机头像(或老用户登录时给其分配一个随机头像)
    public function getDefaultUserHeadPic(){
        $default_avatar=M('default_avatar a');
        $avatar_style=M('avatar_style s');
        $da_where['a.is_deleted']=0;
        $da_where['a.status']=1;
        $da_where['s.is_deleted']=0;
        $da_where['s.status']=1;
        $default_avatar_info=$default_avatar->field("a.id,img as head_pic")
                ->join('left join lc_avatar_style s on a.style_id=s.id')
                ->where($da_where)
                ->select();  
        $cn=count($default_avatar_info);
        $cs=rand(0,$cn-1);
        return $default_avatar_info[$cs]['head_pic'];
    }    

猜你喜欢

转载自www.cnblogs.com/zouke1220/p/9096507.html