config 配置文件
‘cache‘ => [
// 使用复合缓存类型
‘type‘ => ‘complex‘,
// 默认使用的缓存
‘default‘ => [
// 驱动方式
‘type‘ => ‘redis‘,
// 缓存保存目录
‘path‘ => CACHE_PATH,
],
// 文件缓存
‘file‘ => [
// 驱动方式
‘type‘ => ‘file‘,
// 设置不同的缓存保存目录
‘path‘ => RUNTIME_PATH . ‘file/‘,
],
// redis缓存
‘redis‘ => [
// 驱动方式
‘type‘ => ‘redis‘,
// 服务器地址
‘host‘ => ‘127.0.0.1‘,
‘password‘ => ‘root‘,
],
],
控制器
public function index($name,$score){
$redis=new Redis(config(‘redis‘));
$name1=$name.$score;
if(!empty($redis->get($name1))){
$data=$redis->get($name1);
$movie_data=json_decode($data);
return json([‘code‘=>200,‘msg‘=>‘查询成功‘,‘data‘=>$movie_data]);
}
$data=EnterpriseModel::where(‘score‘,‘=‘,$score)->paginate(1);
$acc=json_decode($data);
$redis->set($name,$acc,‘86400‘);
return json([‘code‘=>200,‘msg‘=>‘查询成功‘,‘data‘=>$data]);
}
不用函数截取字符串
public function test($str,$start,$lenght){
//字符串长度
for($strLen=0;;$strLen++){
if(!isset($str[$strLen])){
break;
}
}
$newStr=‘‘;
$j=0;
for($i=0;$i<$strLen;$i++){
if($i>=$start){
if($j==$lenght){
break;
}
$newStr.=$str[$i];
$j++;
}
}
echo $newStr;
}
$obj=new admin();
$obj->test(‘sgVB LWGFHOFHHI‘,1,10);
三要素
function writeJson($result=null,$msg=‘操作成功‘,$errorCode=0,$code=200){
$date=[
‘errorCode‘=>$errorCode,
‘data‘=>$result,
‘msg‘=>$msg
];
return json($date,$code);
}
原文:https://www.cnblogs.com/ahao513/p/14856499.html