Authored by hf

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

... ... @@ -52,6 +52,7 @@ class CacheConfig
const KEY_ACTION_GUANG_PLUSTAR_DATA = 'key_action_guang_plustar_data'; // 逛PLUSTAR
const KEY_ACTION_GUANG_DETAIL_DATA = 'key_action_guang_detail_data'; // 逛内容详情
const KEY_ACTION_GUANG_RSS_DATA = 'key_action_guang_detail_data'; // 逛订阅内容
const KEY_ACTION_GUANG_LIST_DATA = 'key_action_guang_list_data'; // 逛列表内容
const KEY_ACTION_ADDRESS_LIST_DATA = 'key_action_address_list_data'; // 地址树数据
... ...
... ... @@ -42,7 +42,7 @@ class DetailData
$key = CacheConfig::KEY_ACTION_GUANG_DETAIL_DATA . strval($id) . $clientType;
if (USE_CACHE) {
// 先尝试获取二级缓存(slave), 有数据则直接返回.
// 先尝试获取一级缓存(master), 有数据则直接返回.
$cached = Cache::get($key, 'master');
if (!empty($cached)) {
$article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1000);
... ... @@ -56,6 +56,11 @@ class DetailData
// 获取资讯
$article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false);
if (!isset($article['author_id'])) {
// 再尝试获取二级缓存(slave), 有数据则直接返回.
$cached = Cache::get($key, 'slave');
if (!empty($cached)) {
return $cached;
}
return $result;
}
$result['getArticle'] = $article;
... ...
... ... @@ -134,7 +134,7 @@ class PlusstarData
$key = CacheConfig::KEY_ACTION_GUANG_PLUSTAR_DATA . strval($id) . $clientType;
if (USE_CACHE) {
// 先尝试获取二级缓存(slave), 有数据则直接返回.
// 先尝试获取一级缓存(master), 有数据则直接返回.
$cached = Cache::get($key, 'master');
if (!empty($cached)) {
// 判断用户是否已收藏
... ... @@ -151,6 +151,11 @@ class PlusstarData
// 品牌详情信息
$brandInfo = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_PLUSSTAR, 'getBrandInfo', array(array('id' => $id)), 3600); // 缓存1小时
if (!isset($brandInfo['data']['brand_id'])) {
// 再尝试获取二级缓存(master), 有数据则直接返回.
$cached = Cache::get($key, 'slave');
if (!empty($cached)) {
return $cached;
}
return $result;
} else {
$result['getBrandInfo'] = $brandInfo;
... ...
... ... @@ -132,6 +132,15 @@ class IndexModel
// 模板中使用JS的标识
$data['guangHome'] = true;
// 分页需要参数
$data['guang']['gender'] = $gender;
// 显示底部
$data['pageFooter'] = true;
// 缓存键名
$key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('%s_%s_%s', $gender, $type, $page);
// 顶部的分类列表
if (!empty($category['data'])) {
... ... @@ -157,19 +166,19 @@ class IndexModel
$data['guang']['swiper'][] = $build;
}
}
// 只有当$type=0时才有广告参数返回, 所以需要再调一次!!
else {
$temp = ListData::article('1,3', 0, 0, '', $page);
if (!empty($temp['data']['list']['adlist'])) {
foreach ($temp['data']['list']['adlist'] as $value) {
$build = array();
$build['url'] = Helpers::getFilterUrl($value['url']);
$build['img'] = Helpers::getImageUrl($value['src'], 830, 327);
$data['guang']['swiper'][] = $build;
}
}
$temp = array();
}
// // 只有当$type=0时才有广告参数返回, 所以需要再调一次!!
// else {
// $temp = ListData::article('1,3', 0, 0, '', $page);
// if (!empty($temp['data']['list']['adlist'])) {
// foreach ($temp['data']['list']['adlist'] as $value) {
// $build = array();
// $build['url'] = Helpers::getFilterUrl($value['url']);
// $build['img'] = Helpers::getImageUrl($value['src'], 830, 327);
// $data['guang']['swiper'][] = $build;
// }
// }
// $temp = array();
// }
// 内容列表
if (!empty($article['data']['list']['artList'])) {
... ... @@ -177,14 +186,23 @@ class IndexModel
foreach ($article['data']['list']['artList'] as $value) {
$data['guang']['infos'][$type]['info'][] = Helpers::formatArticle($value, true, false, true);
}
if (USE_CACHE) {
$result = Cache::get($key, 'master');
if (empty($result)) {
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
Cache::set($key, $data, 3600);
}
}
}
// 当接口异常时,尝试从二级缓存(slave)获取
elseif (USE_CACHE) {
$result = Cache::get($key, 'slave');
if (!empty($result)) {
return $result;
}
}
// 分页需要参数
$data['guang']['gender'] = $gender;
// 显示底部
$data['pageFooter'] = true;
$category = array();
$article = array();
... ...
... ... @@ -40,7 +40,13 @@ class InfoController extends AbstractAction
}
// 获取详情内容信息, 异常则跳到错误页面
$detail = DetailData::package($id, $isApp);
$detail = array();
try {
$detail = DetailData::package($id, $isApp);
} catch (\Exception $e) {
// do nothing
}
if (empty($detail['getArticle'])) {
$this->_view->display('index', $data);
return;
... ...
... ... @@ -113,7 +113,12 @@ class PlusstarController extends AbstractAction
}
/* 判断品牌信息是否为空 */
$brandInfo = PlusstarData::brandInfo($id, $gender, $uid, $udid, $isApp);
$brandInfo = array();
try {
$brandInfo = PlusstarData::brandInfo($id, $gender, $uid, $udid, $isApp);
} catch (\Exception $e) {
// do nothing
}
if (empty($brandInfo['getBrandInfo']['data']['brand_id'])) {
break;
}
... ...