laravel cache缓存解决访问速度

笑着哭i 提交于 2020-08-15 05:12:14

查询近7天 近30天的数据,并且把每天的数据拿出来生成一个连续多日的折线图:

思路:要拿到每天的数据,循环查询数据库速度很慢,近7天就要查7次,近30天就要查30次,如果一次拿7天或者30天,再循环判断出每天的又需要大改原逻辑,所以想了下,除了今天的数据是实时的,其他时间数据已经不会变了,就把他们存到缓存中,近7天第一天的数据就缓存一天,第二天就缓存两天,因为明天再查近7天,这个第一天就不用了,但是结合还要查近30天,所以近7天的第一天就缓存24天,后面依次推。

代码:

if($time_type==6){
    //组合数据
    $date = [];
    $data = [];
    for ($i=0; $i<=6; $i++){
        $date[$i] = date("n.j" ,strtotime( '+' . $i-6 .' days', time()));

        $time_search_string['start_time'] = strtotime(date("Y-m-d" ,strtotime( '+' . $i-6 .' days', time())));
        $time_search_string['end_time'] = strtotime(date("Y-m-d" ,strtotime( '+' . $i-6 .' days', time())))+86399;

        //如何是今天则取实时数据
        if( $time_search_string['start_time']==strtotime(date("Y-m-d",time())) ){
            $data[$i] = $this->VisitRankData($department_id,$del_type,$time_search_string,$position_status,1);
        }else{
            if (!Cache::has('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1')) {
                $data[$i] = $this->VisitRankData($department_id,$del_type,$time_search_string,$position_status,1);
                Cache::put('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1',json_encode($data[$i]),($i+24)*24*60);
            } else {
                $data[$i]=json_decode(Cache::get('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1'),true);
            }
        }
    }
}else{
    //组合数据
    $date = [];
    $data = [];
    for ($i=0; $i<=29; $i++){
        $date[$i] = date("n.j" ,strtotime( '+' . $i-29 .' days', time()));

        $time_search_string['start_time'] = strtotime(date("Y-m-d" ,strtotime( '+' . $i-29 .' days', time())));
        $time_search_string['end_time'] = strtotime(date("Y-m-d" ,strtotime( '+' . $i-29 .' days', time())))+86399;

        //如何是今天则取实时数据
        if( $time_search_string['start_time']==strtotime(date("Y-m-d",time())) ){
            $data[$i] = $this->VisitRankData($department_id,$del_type,$time_search_string,$position_status,1);
        }else{
            if (!Cache::has('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1')) {
                $data[$i] = $this->VisitRankData($department_id,$del_type,$time_search_string,$position_status,1);
                Cache::put('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1',json_encode($data[$i]),($i+1)*24*60);
            } else {
                $data[$i]=json_decode(Cache::get('visit_echart_'.$department_id.'_'.$del_type.'_'.$time_search_string['start_time'].'_'.$time_search_string['end_time'].'_'.$position_status.'_1'),true);
            }
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!