user cache

This commit is contained in:
toogee 2020-05-15 17:04:04 +08:00
parent f820f252e5
commit 5223d7140b
2 changed files with 16 additions and 5 deletions

View File

@ -233,11 +233,17 @@ abstract class BaseController
} }
//显示用户 //显示当前登录用户
protected function showUser() protected function showUser()
{ {
//1.查询用户 $id = Session::get('user_id');
$user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point,vip,create_time')->where('id',Session::get('user_id'))->cache(600)->find(); $user = Cache::get('user'.$id);
if(!$user){
//1.查询用户
$user = Db::name('user')->field('id,name,nickname,user_img,sex,auth,city,email,sign,point,vip,create_time')->find($id);
Cache::tag('user')->set('user'.$id,$user,600);
}
//2.将User变量赋给模板 公共模板nav.html //2.将User变量赋给模板 公共模板nav.html
View::assign('user',$user); View::assign('user',$user);
} }

View File

@ -113,7 +113,7 @@ class User extends BaseController
$result = Db::name('user') $result = Db::name('user')
->where('id',$userId) ->where('id',$userId)
->update(['user_img'=>$name_path]); ->update(['user_img'=>$name_path]);
Cache::tag(['user','tagArtDetail','tagArt'])->clear();
if($result) { if($result) {
$res = ['status'=>0,'msg'=>'头像更新成功']; $res = ['status'=>0,'msg'=>'头像更新成功'];
} else { } else {
@ -139,7 +139,12 @@ class User extends BaseController
public function home($id) public function home($id)
{ {
//用户 //用户
$u = Db::name('user')->field('name,nickname,city,sex,sign,user_img,point,vip,create_time')->cache(3600)->find($id); $u = Cache::get('user'.$id);
if(!$u){
$u = Db::name('user')->field('name,nickname,city,sex,sign,user_img,point,vip,create_time')->cache(3600)->find($id);
}
//用户发贴 //用户发贴
$arts = Db::name('user')->alias('u')->join('article a','u.id = a.user_id')->field('u.id,a.id,a.title,a.pv,a.is_hot,a.create_time,a.delete_time')->where('a.delete_time',0)->where('a.user_id',$id)->order(['a.create_time'=>'desc'])->cache(3600)->select(); $arts = Db::name('user')->alias('u')->join('article a','u.id = a.user_id')->field('u.id,a.id,a.title,a.pv,a.is_hot,a.create_time,a.delete_time')->where('a.delete_time',0)->where('a.user_id',$id)->order(['a.create_time'=>'desc'])->cache(3600)->select();
//用户回答 //用户回答