Lumen(laravel轻量级)封装redis分页类

data)) {
            $tmp = self::get_page_data($result->data, $page, $size);
        }
        else {
            return $data;
        }

        $result->data = $tmp;

        return json_encode($result);
    }


    //在缓存中读取json内容
    public static function get_cache_data($key)
    {
        $data ='';

        if (empty($key) || $key=="") {
            return null;
        }

        $redis = app('redis')->connection('shan');

        $datas = $redis->get($key);
        if(!empty($datas))//缓存里查找该内容
        {
            $data = $datas;
        }
        if($datas !="")
            return json_decode($data,true);
        else
            return null;
    }


    /**
     * 计算获取数组中的分页数据
     * @param array $data   数据集合
     * @param int   $page   当前页数
     * @param int   $size   每页显示条目
     * @return boolean|array
     */
    public static function get_page_data($data = null, $page = 0, $size = 0) {
        if (empty($data) || !is_array($data)) {
            return null;
        }

        if (!empty($page) && !empty($size)) {
            $count = count($data);  // 计算数组元素数目
            $start = (($page - 1) * $size);  // 开始位,从0开始
            $end = ($start + $size) > $count ? $count : ($start + $size); // 结束位,最大元素数目

            for ($i = $start; $i < $end; $i++) {
                $result[] = $data[$i];
            }
        } else {
            $result = $data;
        }


        return $result;
    }

    /*
     * 将指定数据以指定key值写入redis缓存中
     */
    public static function save_cache_data($key,$data,$expire=86400) {
        $redis = app('redis')->connection('default');
        $redis->set($key, $data); #生成缓存
        $redis->expire($key, $expire);
    }
}

你可能感兴趣的:(Lumen(laravel轻量级)封装redis分页类)