thinkphp配置redis缓存静态内容
配置config.php配置文件
'cache' => [ // 驱动方式 'type' => 'redis', // 缓存前缀 'prefix' => '', // 缓存有效期 0表示永久缓存 'expire' => 7, ], |
think\Cache类具体使用方法
https://www.kancloud.cn/manual/thinkphp5/118131
自己封装的一个缓存存取类
public function getCache($key) { $res = \think\Cache::get($key); if ($res === false) { $hasCache = false; // 是否在switch语句中设置缓存 switch ($key) { case 'index_goods': // 首页底部推荐商品 $res = db('goods')->field('id,cat_id,goodsname,goods_image,price,goods_price')->where("switch", 1)->order("createtime desc")->limit(0, 3)->select(); foreach ($res as $k => $v) { $res[$k]['goods_image'] = $this->request->domain() . $v['goods_image']; } break; case 'index_slide_image': $w['title'] = array('like', '%首页%'); // 首页顶部轮播图片 $add = db('rotation_chart')->field('images')->where($w)->order('createtime desc')->select(); $res = array(); foreach ($add as $k => $v) { $images = explode(',', $v['images']); foreach ($images as $k1 => $v1) { $res[] = $this->request->domain() . $v1; } } break; case 'lollipop_config': // 首页棒棒糖奖励配置 $res = db('lollipop_config')->field('days, step_number, generating_number')->find(); break; case 'shop_index': // 首页商城店铺选项卡 $data = db('rotation_chart')->where(array('title' => '商城', 'switch' => '1'))->find(); $imgarr = explode(",", $data['images']); $images = array(); foreach ($imgarr as $k => $v) { $images[$k]['img'] = 'http://' . $_SERVER['HTTP_HOST'] . $v; } $cat = db('goods_cat')->where(array('switch' => '1'))->select(); foreach ($cat as $k => $v) { $cat[$k]['cat_image'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['cat_image']; $cat[$k]["createtime"] = date('Y-m-d H:i:s', $v["createtime"]); } $goods = db('goods')->field('id, goodsname, goods_image, price, goods_price')->where(array('switch' => '1'))->limit(0,3)->select(); foreach ($goods as $k => $v) { $goods[$k]['goods_image'] = 'http://' . $_SERVER['HTTP_HOST'] . $v['goods_image']; } $res = ([ 'rotationChart' => $images, 'classification' => $cat, 'goods' => $goods, ]); break; case 'service_mobile': // 会员页客服电话 $res = db('config')->where('name', 'service_mobile')->value('value'); break; case 'lolli_count_arr': // 发布棒棒糖订单数量限制 $res = db('salelolli_num')->column('num'); \think\Cache::set($key, $res, 60); // 自定义缓存时间 $hasCache = true; break; case 'paydemo': $res = array('paydemo'=>db('config')->where('name','paydemo')->value('value')); break; } if(!$hasCache){ \think\Cache::set($key, $res, 3600); // 缓存一个小时 } } return $res; } |