阅读 82

thinkphp5+redis 存值取值

 

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

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐