作者 liyuhang

gx

@@ -72,9 +72,13 @@ class Common @@ -72,9 +72,13 @@ class Common
72 * @method 72 * @method
73 */ 73 */
74 public static function get_user_cache($table,$id,$type = 'B'){ 74 public static function get_user_cache($table,$id,$type = 'B'){
  75 + $data = [];
  76 + $cache = config('cache.user_is_cache');
  77 + if(isset($cache) && ($cache['is_cache'] == true)){
75 $key = 'cache_'.$table.'_'.$id.'_type'; 78 $key = 'cache_'.$table.'_'.$id.'_type';
76 - $lists = Cache::store('file')->get($key);  
77 - return $lists; 79 + $data = Cache::store('file')->get($key);
  80 + }
  81 + return $data;
78 } 82 }
79 83
80 /** 84 /**
@@ -84,8 +88,11 @@ class Common @@ -84,8 +88,11 @@ class Common
84 * @method 88 * @method
85 */ 89 */
86 public static function set_user_cache($data = [],$table,$id,$type = 'B'){ 90 public static function set_user_cache($data = [],$table,$id,$type = 'B'){
  91 + $cache = config('cache.user_is_cache');
  92 + if(isset($cache) && ($cache['is_cache'] == true)){
87 $key = 'cache_'.$table.'_'.$id.'_type'; 93 $key = 'cache_'.$table.'_'.$id.'_type';
88 Cache::store('file')->set($key,$data,3600); 94 Cache::store('file')->set($key,$data,3600);
  95 + }
89 return true; 96 return true;
90 } 97 }
91 98
@@ -96,11 +103,15 @@ class Common @@ -96,11 +103,15 @@ class Common
96 * @method 103 * @method
97 */ 104 */
98 public static function del_user_cache($table,$id = '',$type = 'B'){ 105 public static function del_user_cache($table,$id = '',$type = 'B'){
  106 + $cache = config('cache.user_is_cache');
  107 + if(isset($cache) && ($cache['is_cache'] == true)){
99 if (!empty($id)){ 108 if (!empty($id)){
100 $key = 'cache_'.$table.'_'.$id.'_type'; 109 $key = 'cache_'.$table.'_'.$id.'_type';
101 }else{ 110 }else{
102 $key = 'cache_'.$table.'_*'; 111 $key = 'cache_'.$table.'_*';
103 } 112 }
104 - return Cache::store('file')->pull($key); 113 + Cache::store('file')->pull($key);
  114 + }
  115 + return true;
105 } 116 }
106 } 117 }
@@ -117,6 +117,8 @@ class BlogLogic extends BaseLogic @@ -117,6 +117,8 @@ class BlogLogic extends BaseLogic
117 } 117 }
118 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']); 118 RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $this->param['id'], $this->user['project_id']);
119 $this->model->edit($this->param,['id'=>$this->param['id']]); 119 $this->model->edit($this->param,['id'=>$this->param['id']]);
  120 + //清除缓存
  121 + Common::del_user_cache($this->model->getTable(),$this->param['id']);
120 DB::commit(); 122 DB::commit();
121 }catch (\Exception $e){ 123 }catch (\Exception $e){
122 DB::rollBack(); 124 DB::rollBack();
@@ -136,6 +138,8 @@ class BlogLogic extends BaseLogic @@ -136,6 +138,8 @@ class BlogLogic extends BaseLogic
136 if($rs === false){ 138 if($rs === false){
137 $this->fail('error'); 139 $this->fail('error');
138 } 140 }
  141 + //清除缓存
  142 + Common::del_user_cache($this->model->getTable(),$this->param['id']);
139 return $this->success(); 143 return $this->success();
140 } 144 }
141 /** 145 /**
@@ -145,19 +149,23 @@ class BlogLogic extends BaseLogic @@ -145,19 +149,23 @@ class BlogLogic extends BaseLogic
145 * @method 149 * @method
146 */ 150 */
147 public function blog_info(){ 151 public function blog_info(){
  152 + //读取缓存
  153 + $info = Common::get_user_cache($this->model->getTable(),$this->param['id']);
  154 + if(!empty($info)){
  155 + return $this->success($info);
  156 + }
148 $info = $this->model->read($this->param); 157 $info = $this->model->read($this->param);
149 if($info === false){ 158 if($info === false){
150 $this->fail('error'); 159 $this->fail('error');
151 } 160 }
152 //获取分类名称 161 //获取分类名称
153 - $cate = explode(',',trim($info['category_id'],','));  
154 - $blogCategoryModel = new BlogCategoryModel();  
155 - $category_list = $blogCategoryModel->list(['id'=>['in',$cate]],'id',['name']);  
156 - $str = '';  
157 - foreach ($category_list as $v){  
158 - $str .= $v['name'].',';  
159 - }  
160 - $info['category_name'] = trim($str,','); 162 + $blogCategoryLogic = new BlogCategoryLogic();
  163 + $info = $blogCategoryLogic->get_category_name($info);
  164 + //获取标签名称
  165 + $blogLabelLogic = new BlogLabelLogic();
  166 + $info = $blogLabelLogic->get_label_name($info);
  167 + //写入缓存
  168 + Common::set_user_cache($info,$this->model->getTable(),$this->param['id']);
161 return $this->success($info); 169 return $this->success($info);
162 } 170 }
163 171
@@ -104,22 +104,11 @@ class Base extends Model @@ -104,22 +104,11 @@ class Base extends Model
104 */ 104 */
105 public function read($condition,$files = ['*']) 105 public function read($condition,$files = ['*'])
106 { 106 {
107 - //读取缓存  
108 - $cache = config('cache.user_is_cache');  
109 - if(($cache['is_cache'] == true) && isset($condition['id'])){  
110 - $info = Common::get_user_cache($this->getTable(),$condition['id']);  
111 - if(!empty($info)){  
112 - return $info;  
113 - }  
114 - }  
115 $query = $this->formatQuery($condition); 107 $query = $this->formatQuery($condition);
116 $info = $query->select($files)->first(); 108 $info = $query->select($files)->first();
117 if (empty($info)) { 109 if (empty($info)) {
118 return false; 110 return false;
119 } 111 }
120 - if(($cache['is_cache'] == true) && isset($condition['id'])){  
121 - Common::set_user_cache($info,$this->getTable(),$condition['id']);  
122 - }  
123 $info = $info->toArray(); 112 $info = $info->toArray();
124 return $info; 113 return $info;
125 } 114 }
@@ -143,13 +132,8 @@ class Base extends Model @@ -143,13 +132,8 @@ class Base extends Model
143 public function edit($data,$condition){ 132 public function edit($data,$condition){
144 $query = $this->formatQuery($condition); 133 $query = $this->formatQuery($condition);
145 $data['updated_at'] = date('Y-m-d H:i:s'); 134 $data['updated_at'] = date('Y-m-d H:i:s');
146 - $rs = $query->update($data);  
147 - //更新缓存  
148 - $cache = config('cache.user_is_cache');  
149 - if(($cache['is_cache'] == true) && isset($condition['id'])) {  
150 - Common::del_user_cache($this->getTable(), $condition['id']);  
151 - }  
152 - return $rs; 135 + return $query->update($data);
  136 +
153 } 137 }
154 /** 138 /**
155 * @name : 删除数据 139 * @name : 删除数据