laravel读取memcached缓存并做条件查询

public function onlineplayersource()
{
  $res = $_POST['aoData'];
  $sEcho = 0;
  $iDisplayStart = 0; // 起始索引
  $iDisplayLength = 0;//分页长度
  $jsonarray= json_decode($res) ;

  $start = $jsonarray->start;//intval($res['start']);
  $length = $jsonarray->length;//intval($res['length']);
  $order = $jsonarray->order[0];
  $orderColumn = $jsonarray->columns[$order->column]->data;
  $OnlineRoom = $jsonarray->form_param->OnlineRoom;
  $user_name = $jsonarray->form_param->user_name;
  $startdate = $jsonarray->form_param->startdate;
  $enddate = $jsonarray->form_param->enddate;
 
  $r = [];
  $memcache = new Memcache();
  $mem_ip = config('cache.stores')['memcached']['servers'][0]['host'];
  $mem_port = config('cache.stores')['memcached']['servers'][0]['port'];

  $sql = "select * FROM [dbo].[fn_GetUserOnline] () d where d.OnlineRoom is not null";
  $key = "online";
  try {
    $memcache->connect($mem_ip,$mem_port);
    $cac = $memcache->get($key);
    if (!$cac) {
      $data = DB::select($sql);//->skip($start)->take($length)->get()->toArray();
      $total = DB::select($cnt_sql);
      $cnt = $total[0]->cnt;
      if($memcache->set($key,['data'=>$data, 'cnt'=>$total[0]->cnt], false, 300))
      {
      }
      $r['from_cache'] = false;
    } else {
      $data = $cac['data'];
      $cnt = count($data);
      $r['from_cache'] = true;
    }
  } catch(Exception $e) {
    $data = DB::select($sql);//->skip($start)->take($length)->get()->toArray();
    $total = DB::select($cnt_sql);
    $cnt = $total[0]->cnt;
    $r['from_cache'] = false;
    $r['exception'] = true;
  }
  $r['orderColumn'] = $orderColumn;
  $r['orderDir'] = $orderDir;
  $r['start'] = $start;
  $r['length'] = $length;
 
  $ds = $this->process($data, $orderColumn, $orderDir, $user_name, $OnlineRoom, $startdate, $enddate);
  $r['ori'] = $ds;
  $cnt = count($ds);
  $ds = $ds->slice($start, $length);
  $ds = $ds->values()->all();

  $r['ds'] = $ds;
  $data = $ds;

  $ipobj = new AppHttpModelIP();
  foreach ($ds as $player) {
    $addr = $ipobj->ip2addr($player->LastLoginIP);
    if (isset($addr)) {
      $player->LastLoginIPText = '【'.$addr['country'].'】'.$addr['area'];
    } else {
      $player->LastLoginIPText = '';
    }
  }
 
  $r['data'] = $data;
 
  $r['iTotalDisplayRecords'] = $cnt;
  $r['iTotalRecords'] = $cnt;

  return json_encode($r);
}
 
private function process($arr, $order, $orderDir, $user_name, $OnlineRoom, $startdate, $enddate) {
  $col = collect($arr);
  if ($orderDir == 'desc')
    $sorted = $col->sortByDesc($order);
  else
    $sorted = $col->sortBy($order);
  $sorted = $sorted->filter(function ($value, $key) use($user_name, $OnlineRoom, $startdate, $enddate) {
    $result = true;
    if (isset($value->UserName) && strlen($value->UserName) > 0 && isset($user_name) && strlen($user_name) > 0) {
      $position = strpos($value->UserName, $user_name);
      if ($position === false)
        $result = false;
      else
        $result = true;
    }

    $result_uid = true;
    if (isset($value->UserID) && strlen($value->UserID) > 0 && isset($user_name) && strlen($user_name) > 0) {
      if ($value->UserID != $user_name) {
        $result_uid = false;
      }
    }
    $result = $result || $result_uid;

    $result_onlineroom = true;
    if (isset($value->OnlineRoom) && strlen($value->OnlineRoom) > 0 && isset($OnlineRoom) && strlen($OnlineRoom) > 0) {
      if ($value->OnlineRoom != $OnlineRoom) {
        $result_onlineroom = false;
      }
    }
    $result = $result && $result_onlineroom;

    $result2 = true;
    if (isset($value->LastLoginTM) && strlen($value->LastLoginTM) > 0 && isset($startdate) && strlen($startdate) > 0) {
      if ($startdate && $startdate != '') {
        $result2 = strtotime($value->LastLoginTM) > strtotime($startdate);
      }
    }
    $result = $result && $result2;

    $result3 = true;
    if (isset($value->LastLoginTM) && strlen($value->LastLoginTM) > 0 && isset($enddate) && strlen($enddate) > 0) {
      if ($enddate && $enddate != '') {
        $result3 = strtotime($value->LastLoginTM) <= strtotime($enddate);
      }
    }
    $result = $result && $result3;
    return $result;
  });
  //$sorted = $sorted->where('IsRobot', 0);
  return $sorted;
}
原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/8011675.html