1. 程式人生 > >PHP處理大數據量老用戶頭像更新的操作

PHP處理大數據量老用戶頭像更新的操作

time action dpi rac cat -- left AC lec

    /**
     * @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];
    }    

PHP處理大數據量老用戶頭像更新的操作