From ec3f0b3a3d19a65696c60394bfbe23aecdd269d2 Mon Sep 17 00:00:00 2001 From: zhao Date: Wed, 11 Mar 2020 11:33:17 +0800 Subject: [PATCH] article cache --- app/common.php | 8 +------- app/common/controller/BaseController.php | 4 ++-- app/index/controller/Article.php | 9 ++++++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/common.php b/app/common.php index bb63e4f..76c34f0 100644 --- a/app/common.php +++ b/app/common.php @@ -76,13 +76,7 @@ if(!function_exists('getUserImg')) //根据文章分类ID查询分类名 function getCateName($cate_ename) { - $cname = Cache::get('cname'); - if(!$cname){ - $cname = Db::name('cate')->where('ename',$cate_ename)->value('catename'); - Cache::set('cname',$cname,3600); - } - return $cname; - // return Db::name('cate')->where('ename',$cate_ename)->value('catename'); + return Db::name('cate')->where('ename',$cate_ename)->value('catename'); } diff --git a/app/common/controller/BaseController.php b/app/common/controller/BaseController.php index 255a329..12df70d 100644 --- a/app/common/controller/BaseController.php +++ b/app/common/controller/BaseController.php @@ -226,7 +226,7 @@ abstract class BaseController protected function showNav() { //1.查询分类表获取所有分类 - $cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->cache(120)->select(); + $cateList = Db::name('cate')->where(['status'=>1,'delete_time'=>0])->order('sort','asc')->cache(3600)->select(); //2.将catelist变量赋给模板 公共模板nav.html View::assign('cateList',$cateList); @@ -246,7 +246,7 @@ abstract class BaseController protected function showSystem() { //1.查询分类表获取所有分类 - $sysInfo = Db::name('system')->cache(120)->find(1); + $sysInfo = Db::name('system')->cache(3600)->find(1); View::assign('sysInfo',$sysInfo); } diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php index a35eee9..2756ab0 100644 --- a/app/index/controller/Article.php +++ b/app/index/controller/Article.php @@ -5,6 +5,7 @@ use app\common\controller\BaseController; use think\facade\View; use think\facade\Request; use think\facade\Db; +use think\facade\Cache; use app\common\model\Cate; use app\common\model\User; use app\common\model\Comment; @@ -94,9 +95,9 @@ class Article extends BaseController //文章详情页 public function detail($id) { - //获取文章ID - //$id = Request::param('id'); - //查询文章 + $article = Cache::get('article'); + if(!$article){ + //查询文章 $article = ArticleModel::field('id,title,content,status,cate_id,user_id,is_top,is_hot,is_reply,pv,jie,tags,create_time')->where('status',1)->with([ 'cate' => function($query){ $query->where('delete_time',0)->field('id,catename'); @@ -105,6 +106,8 @@ class Article extends BaseController $query->field('id,name,nickname,user_img,area_id'); } ])->find($id); + Cache::set('article',$article,3600); + } $comments = $article->comments()->where('status',1)->select(); $article->inc('pv')->update();