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 \App\Http\Model\IP();
  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;
}

laravel读取memcached缓存并做条件查询的更多相关文章

  1. mysql拆分字符串做条件查询

    mysql拆分字符串作为查询条件 有个群友问一个问题 这表的ancestors列存放的是所有的祖先节点,以,分隔 例如我查询dept_id为103的所有祖先节点,现在我只有一个dept_id该怎么查 ...

  2. 用Set中元素做条件查询

    一个老师教许多学生,一个学生被许多老师教,一个学生有好多书,同一种书被许多同学拥有.查询教拥有书"a"的学生的老师:   class teacher{   String id;   ...

  3. laravel 根据字段不同值做不同查询

    在开发过程中我们经常遇到这种情况: 例如,一个信息表message,字段type 1.操作提醒 2.平台通知,表message_read记录当信息是平台通知时用户浏览状况 那么 当信息是平台通知时是针 ...

  4. 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma

     1 PersonTestMapper.xml中的内容如下: <?xmlversion="1.0"encoding="UTF-8"?> < ...

  5. 应对Memcached缓存失效,导致高并发查询DB的四种思路(l转)

    当Memcached缓存失效时,容易出现高并发的查询DB,导致DB压力骤然上升. 这篇blog主要是探讨如何在缓存将要失效时,及时地更新缓存,而不是如何在缓存失效之后,如何防止高并发的DB查询. 解决 ...

  6. 应对Memcached缓存失效,导致高并发查询DB的几种思路

    原文地址: http://blog.csdn.net/hengyunabc/article/details/20735701 当Memcached缓存失效时,容易出现高并发的查询DB,导致DB压力骤然 ...

  7. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  8. Laravel条件查询数据单条数据first,多条数据get

    使用DB查询,必须use Illuminate\Support\Facades\DB; 多数组条件查询单条数据 first() //提交加入我们数据 public function ajax_join ...

  9. Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例

    单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...

随机推荐

  1. 用grep 筛选fastq 序列

    grep 从文件中筛选出 包含指定的字符或者正则表达式的行:默认只打印匹配到的行, 比如一个文件 test.txt, 其内容为: abc def ghi jkl grep a test.txt, 输出 ...

  2. C# 最小化到托盘,托盘右击菜单显示

    添加notifyIcon控件,并添加Icon,否则托盘没有图标(托盘右键菜单也可直接在属性里添加): 主要的代码: public partial class Form1 : Form { #regio ...

  3. 分享10款效果惊艳的HTML5图片特效

    在HTML5的世界里,图片特效都十分绚丽,我们在网站上也分享过很多不错的HTML5图片特效,现在我们精选10款效果惊艳的HTML5图片特效分享给大家. 1.HTML5 3D正方体旋转动画 很酷的3D特 ...

  4. JSTL的if...else项目小试

      最近在项目中有一个小的效果显示为:在前端,根据一个字段来判断是否弹出一个窗口. 具体需求为:单击表格中的课程名称链接,如果此课程已经被排课,那么就弹出排课窗口,如果未排课就弹出提示box. 具体的 ...

  5. qt creator如何实现转到槽功能

    ui_mainwindow.h .

  6. 处理执行sql语句

    sqlplus / as sysdba @1.sqlsqlplus / as sysdba @2.sqlsqlplus / as sysdba @3.sql

  7. 缓存管理(本地缓存+memcached)

    http://www.cnblogs.com/daizhj/archive/2009/11/17/1604436.html

  8. 九度 1464:Hello World for U

    题目描述: Given any string of N (>=5) characters, you are asked to form the characters into the shape ...

  9. RFC文件

    RFC(Request For Comments)-意即“请求评议”,包含了关于Internet的几乎所有重要的文字资料.如果你想成为网络方面的专家,那么RFC无疑是最重要也是最经常需要用到的资料之一 ...

  10. DokuWiki 开源wiki引擎程序

    DokuWiki是一个开源wiki引擎程序,运行于PHP环境下.DokuWiki程序小巧而功能强大.灵活,适合中小团队和个人网站知识库的管理. 官网:https://www.dokuwiki.org/ ...