thinkphp5生成文件缓存,redis缓存

thinkphp5生成文件缓存,redis缓存_第1张图片

thinkphp5生成文件缓存,redis缓存_第2张图片

thinkphp5生成文件缓存,redis缓存_第3张图片

[
    // 驱动方式
    'type'   => 'File',
    // 缓存保存目录
    'path'   => CACHE_PATH,
    // 缓存前缀
    'prefix' => '',
    // 缓存有效期 0表示永久缓存
    'expire' => 0,
],

配置文件

 

thinkphp5生成文件缓存,redis缓存_第4张图片

thinkphp5生成文件缓存,redis缓存_第5张图片

 

缓存一个分类 

   public function cachetype(){
       $options=[
           // 驱动方式
           'type'   => 'Redis',
           // 缓存保存目录
           'path'   => CACHE_PATH,
           // 缓存前缀
           'prefix' => '',
           // 缓存有效期 0表示永久缓存
           'expire' => 0,
       ]; //将config.php文件中默认配置改成redis缓存
       $redis=Cache::connect($options);

      //$redis->rm('types');//清除缓存

       if(!$redis->get('types')){ //如果redis缓存中没有则查询数据库
           $type1=Db::name('goods_class')->where(array('pid'=>0))->field('id,pid,name')->select();//获取一级分类
           $type2=array();
           $type3=array();
           foreach ($type1 as $k=>$v){
               $type1[$k]['child']=array();
               $type2=Db::name('goods_class')->where(array('pid'=>$v['id']))->field('id,pid,name')->select();//获取二级分类
               foreach ($type2 as $key=>$val){

                   array_push($type1[$k]['child'],$val);//合并一二级分类
                   $type1[$k]['child'][$key]['child2']=array();//组装三级分类的数组
                   $type3=Db::name('goods_class')->where(array('pid'=>$val['id']))->field('id,pid,name')->select();//获取三级分类
                   foreach ($type3 as $v2){
                       array_push($type1[$k]['child'][$key]['child2'],$v2);
                   }
               }

           }

           //查询完后缓存
           $redis->set('types',$type1);

       }else{
           $type1=$redis->get('types');
       
       }
       return $type1;
   }

 

先要开启服务端redis-server

开启客户端redis-cli

测试

 

thinkphp5生成文件缓存,redis缓存_第6张图片

thinkphp5生成文件缓存,redis缓存_第7张图片

 

thinkphp5生成文件缓存,redis缓存_第8张图片

你可能感兴趣的:(thinkphp5,redis,php缓存)