article cache

This commit is contained in:
zhao 2020-03-11 11:33:17 +08:00
parent dde58e385a
commit ec3f0b3a3d
3 changed files with 9 additions and 12 deletions

View File

@ -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');
}

View File

@ -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);
}

View File

@ -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();