laravel + memcache 截止到目前为止12小时内的数据
来源:互联网 发布:java框架面试题 编辑:程序博客网 时间:2024/06/11 12:51
思路:获取当前时间 ,并拿到16点0的时间点,依次类推拿到15点 - 6点 的时间段统计的数据
以时间点为cachekey,第二次读取到的就不需要重复统计
部分代码:
获取时间节点的方法
// 时间节点 时间集合
public static function Tpoinsections($start,$hours)
{
//Interevel = 600 10min allhours =168 ,
$timeSections = [];
$timeIntervel ;
$startopoint = $start;
switch ($hours) {
case 1:
$timeIntervel = 10*60;
$m = floor(date("i", $start)/10)*10;
$startopoint = strtotime(date("Y-m-d H:".$m.":0", $start));
break;
case 8:
$timeIntervel = 3600;
$startopoint = strtotime(date("Y-m-d H",$start).":0:0");
break;
case 12:
$timeIntervel = 3600;
$startopoint = strtotime(date("Y-m-d H",$start).":0:0");
break;
case 168:
$timeIntervel = 24*3600;
$startopoint = strtotime(date("Y-m-d",$start)."0:0:0");
break;
default:
break;
}
$count = $hours*3600/$timeIntervel;//得到多少刻度
// [0]:($startpoint,$start); [1] $starpoint - hours
$timeSections[0]=[$start,$startopoint];
for ($i= 1; $i < $count ; $i++) {
$timeSections[$i]=[intval($startopoint)-($i-1)*$timeIntervel,intval($startopoint)-$i*$timeIntervel];
}
return $timeSections;
}
/**
*$len: time scale ;$day:
*/
// 缓存
public static function cahedata($sql,$where2,$timeSections,$cacheKey,$ylen,$hours)
{
$result= [];
$from = [];
$categories = [];
$cacheKeys=[];
$strdate='Y-m-d H:i:s';
$Expire = 1800;
foreach($timeSections as $i=> $timeSection) {
$timsec1 = $timeSection[0];
$categories[$i] = date($strdate,$timsec1);
if (\Cache::has($cacheKey.$timsec1)) {
$cacheKeys[$i] =$cacheKey.$timsec1;
$perCacheResult = \Cache::get( $cacheKey.$timsec1 );
$result[$i] = $perCacheResult;
$from[$i]='cached'.$timsec1;
}
else{
$where = " created_at > $timeSection[1] and created_at < $timeSection[0] ";
$sql2= $sql.$where.$where2;
$perResult = DB::select($sql2);
if($i!=0){ //当前第一个时间不缓存
\Cache::put($cacheKey.$timsec1,$perResult,$Expire);
}
$result[$i] = $perResult;
$from[$i]='db'.$timsec1;
}
}
//处理数据
$infos = json_encode($result);
$topinfo =json_decode($infos,true);
//stdClass转成数组,重点
$topinfo = (array)array_map(function($item){
return array_pluck($item, 'y');
}, $topinfo);
$result = array_pluck($topinfo, '0');//mysql语句 count(table_filed) as y
$data = array('result'=>$result,'from'=>$from,'date'=>$categories);
return $data;
}
- laravel + memcache 截止到目前为止12小时内的数据
- 到目前为止
- CSDN截止目前为止有多少的博客专家以及比例人数?
- 截止到目前我所用到的第三方库
- 不懂的东西永远是 “到目前为止”。
- 到目前为止的进展情况(v.1.0.0.4)
- 飞鸽传书软件来说到目前为止最难走的路
- 人类到目前为止发现的唯一一个素数公式
- 浅谈自己到目前为止的AVA学习经历
- 第五章到目前为止学过的
- 到目前为止Delphi 2006中ECO多客户端技术的能与不能
- 到目前为止,我最喜欢的一部电影,我已经看了几十次了。
- ColorPix——到目前为止最好用的屏幕取色器
- 到目前为止,Linux下最完整的Samba服务器配置攻略
- 历史工作记录:V8-SH4移植项目到目前为止的经验总结 2010.3
- 到目前为止,Linux下最完整的Samba服务器配置攻略
- 到目前为止,Linux下最完整的Samba服务器配置攻略
- 到目前为止,Linux下最完整的Samba服务器配置攻略
- 流媒体-FLV格式详解及数据分析
- centOS 7下搭建Redis服务器,并使用Java访问操作Redis
- 谈HDU2087收获
- Java线程池的分析和使用
- Java_Ant详解
- laravel + memcache 截止到目前为止12小时内的数据
- 高通使用/system/bin/r读取msm8916的gpio配置
- 读取本地文件
- Java代码中常见技术债务处理之Exception
- NYOJ 216 An problem about data
- 既使用maven编译,又使用lib下的Jar包
- Writer 类常用方法
- Python进阶(三十一)-往MySQL数据库里添加数据,update和insert哪个效率高
- Android混淆配置