thinkphp5生成文件缓存,redis缓存

18 篇文章 0 订阅
4 篇文章 0 订阅

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

配置文件

 

 

缓存一个分类 

   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

测试

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芝麻开门2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值