Authored by hf

code review by hf: do format guang code handle call api exception use two level cache

@@ -269,6 +269,50 @@ class IndexModel @@ -269,6 +269,50 @@ class IndexModel
269 269
270 return $result; 270 return $result;
271 } 271 }
  272 +
  273 + /**
  274 + * 逛标签列表或作者内容列表
  275 + *
  276 + * @param string $gender "1,3"表示男, "2,3"表示女, "1,2,3"表示所有
  277 + * @param int $sortId 分类ID
  278 + * @param int $uid 用户ID
  279 + * @param string $udid 客户端唯一标识
  280 + * @param int $page 分页第几页, 默认第1页
  281 + * @param string $tag 标签
  282 + * @param int $authorId 作者ID
  283 + * @return array
  284 + */
  285 + public static function getArticleByTagOrEditor($gender, $sortId, $uid = 0, $udid = '', $page = 1, $tag = null, $authorId = null)
  286 + {
  287 + // 缓存键名
  288 + if (isset($tag)) {
  289 + $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('tag_%s_%s_%s_%s', $gender, $sortId, $page, $tag);
  290 + } else {
  291 + $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('editor_%s_%s_%s_%s', $gender, $sortId, $page, $authorId);
  292 + }
  293 +
  294 + // 调用接口,获取内容
  295 + $article = ListData::article($gender, $sortId, $uid, $udid, $page, $tag, $authorId);
  296 + // 内容列表
  297 + if (!empty($article['data']['list']['artList'])) {
  298 + if (USE_CACHE) {
  299 + $result = Cache::get($key, 'master');
  300 + if (empty($result)) {
  301 + // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
  302 + Cache::set($key, $article, 3600);
  303 + }
  304 + }
  305 + }
  306 + // 当接口异常时,尝试从二级缓存(slave)获取
  307 + elseif (USE_CACHE) {
  308 + $result = Cache::get($key, 'slave');
  309 + if (!empty($result)) {
  310 + return $result;
  311 + }
  312 + }
  313 +
  314 + return $article;
  315 + }
272 316
273 /** 317 /**
274 * 构建信息内容 318 * 构建信息内容
@@ -90,7 +90,8 @@ class IndexController extends AbstractAction @@ -90,7 +90,8 @@ class IndexController extends AbstractAction
90 } 90 }
91 91
92 // 标签聚合内容列表 92 // 标签聚合内容列表
93 - $article = ListData::article($gender, 0, $uid, $udid, 1, $tag); 93 + //$article = ListData::article($gender, 0, $uid, $udid, 1, $tag);
  94 + $article = Guang\IndexModel::getArticleByTagOrEditor($gender, 0, $uid, $udid, 1, $tag);
94 // 标签聚合内容不存在, 跳到错误页面 95 // 标签聚合内容不存在, 跳到错误页面
95 if (empty($article['data']['list']['artList'])) { 96 if (empty($article['data']['list']['artList'])) {
96 $this->error(); 97 $this->error();
@@ -182,7 +183,8 @@ class IndexController extends AbstractAction @@ -182,7 +183,8 @@ class IndexController extends AbstractAction
182 $data['authorInfo']['id'] = $id; 183 $data['authorInfo']['id'] = $id;
183 184
184 // 标签聚合内容列表 185 // 标签聚合内容列表
185 - $article = ListData::article($gender, 0, $uid, $udid, 1, null, $id); 186 + //$article = ListData::article($gender, 0, $uid, $udid, 1, null, $id);
  187 + $article = Guang\IndexModel::getArticleByTagOrEditor($gender, 0, $uid, $udid, 1, null, $id);
186 // 构建资讯文章内容 188 // 构建资讯文章内容
187 if (!empty($article['data']['list']['artList'])) { 189 if (!empty($article['data']['list']['artList'])) {
188 $build = array(); 190 $build = array();
@@ -267,7 +269,7 @@ class IndexController extends AbstractAction @@ -267,7 +269,7 @@ class IndexController extends AbstractAction
267 } 269 }
268 270
269 /** 271 /**
270 - * @todo 清缓存 272 + * 内部清缓存
271 */ 273 */
272 public function clearAction() 274 public function clearAction()
273 { 275 {
@@ -278,39 +280,46 @@ class IndexController extends AbstractAction @@ -278,39 +280,46 @@ class IndexController extends AbstractAction
278 280
279 $type = $this->get('type'); 281 $type = $this->get('type');
280 switch (strval($type)) { 282 switch (strval($type)) {
281 - case 'boys': 283 + case 'boys': // 男首
282 Cache::delete(CacheConfig::KEY_ACTION_BOYS_INDEX); 284 Cache::delete(CacheConfig::KEY_ACTION_BOYS_INDEX);
283 break; 285 break;
284 - case 'girls': 286 + case 'girls': // 女首
285 Cache::delete(CacheConfig::KEY_ACTION_GIRLS_INDEX); 287 Cache::delete(CacheConfig::KEY_ACTION_GIRLS_INDEX);
286 break; 288 break;
287 - case 'kids': 289 + case 'kids': // 潮童
288 Cache::delete(CacheConfig::KEY_ACTION_KIDS_INDEX); 290 Cache::delete(CacheConfig::KEY_ACTION_KIDS_INDEX);
289 break; 291 break;
290 - case 'lifestyle': 292 + case 'lifestyle': // 创意生活
291 Cache::delete(CacheConfig::KEY_ACTION_LIFESTYLE_INDEX); 293 Cache::delete(CacheConfig::KEY_ACTION_LIFESTYLE_INDEX);
292 break; 294 break;
293 - case 'bgimg': 295 + case 'bgimg': // 频道选择背景图
294 Cache::delete(CacheConfig::KEY_ACTION_INDEX_INDEX); 296 Cache::delete(CacheConfig::KEY_ACTION_INDEX_INDEX);
295 break; 297 break;
296 - case 'brands': 298 + case 'brands': // 品牌一览
297 Cache::delete('3206a153b0cebbd6e47be90c6ac41ee8'); 299 Cache::delete('3206a153b0cebbd6e47be90c6ac41ee8');
298 Cache::delete('1c6bb62c480aec413b247d6f21e9c23a'); 300 Cache::delete('1c6bb62c480aec413b247d6f21e9c23a');
299 Cache::delete('60bb7899f49b3549dde438ac53526dea'); 301 Cache::delete('60bb7899f49b3549dde438ac53526dea');
300 Cache::delete('9085ba21ac3d4f849c977da37a290a23'); 302 Cache::delete('9085ba21ac3d4f849c977da37a290a23');
301 break; 303 break;
302 - case 'new': 304 + case 'new': // 新品
303 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '3cf2c1be5217fbab6009ce83959e1e12'); 305 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '3cf2c1be5217fbab6009ce83959e1e12');
304 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '1cf7f9f10e2a2670e73d05c568793ad9'); 306 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '1cf7f9f10e2a2670e73d05c568793ad9');
305 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '57457adececa6c748b29c90cad0ae940'); 307 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '57457adececa6c748b29c90cad0ae940');
306 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '04953a61cbf1db426a681e55d496d2fe'); 308 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_INDEX . '04953a61cbf1db426a681e55d496d2fe');
307 break; 309 break;
308 - case 'sale': 310 + case 'sale': // SALE
309 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '153180b9a88c0b565848850c523bb637'); 311 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '153180b9a88c0b565848850c523bb637');
310 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '0b2d133419a0f7c381306fd3522365e1'); 312 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '0b2d133419a0f7c381306fd3522365e1');
311 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . 'de23648d28ee1e8a3f087a9dbac506f8'); 313 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . 'de23648d28ee1e8a3f087a9dbac506f8');
312 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '01269e498ff5b07756e0733ec0e88c75'); 314 Cache::delete(CacheConfig::KEY_ACTION_PRODUCT_NEWSALE_DISCOUNT . '01269e498ff5b07756e0733ec0e88c75');
313 break; 315 break;
  316 + case 'nav': // 首页左侧边栏
  317 + Cache::delete(CacheConfig::KEY_COMMON_SIDE_NAV);
  318 + break;
  319 + case 'index': // 频道选择
  320 + Cache::delete(CacheConfig::KEY_ACTION_INDEX_INDEX);
  321 + Cache::delete(CacheConfig::KEY_ACTION_INDEX_CONFIG);
  322 + break;
314 } 323 }
315 } 324 }
316 325