code review by hf: do format guang code handle call api exception use two level cache
Showing
6 changed files
with
61 additions
and
21 deletions
@@ -52,6 +52,7 @@ class CacheConfig | @@ -52,6 +52,7 @@ class CacheConfig | ||
52 | const KEY_ACTION_GUANG_PLUSTAR_DATA = 'key_action_guang_plustar_data'; // 逛PLUSTAR | 52 | const KEY_ACTION_GUANG_PLUSTAR_DATA = 'key_action_guang_plustar_data'; // 逛PLUSTAR |
53 | const KEY_ACTION_GUANG_DETAIL_DATA = 'key_action_guang_detail_data'; // 逛内容详情 | 53 | const KEY_ACTION_GUANG_DETAIL_DATA = 'key_action_guang_detail_data'; // 逛内容详情 |
54 | const KEY_ACTION_GUANG_RSS_DATA = 'key_action_guang_detail_data'; // 逛订阅内容 | 54 | const KEY_ACTION_GUANG_RSS_DATA = 'key_action_guang_detail_data'; // 逛订阅内容 |
55 | + const KEY_ACTION_GUANG_LIST_DATA = 'key_action_guang_list_data'; // 逛列表内容 | ||
55 | 56 | ||
56 | const KEY_ACTION_ADDRESS_LIST_DATA = 'key_action_address_list_data'; // 地址树数据 | 57 | const KEY_ACTION_ADDRESS_LIST_DATA = 'key_action_address_list_data'; // 地址树数据 |
57 | 58 |
@@ -42,7 +42,7 @@ class DetailData | @@ -42,7 +42,7 @@ class DetailData | ||
42 | $key = CacheConfig::KEY_ACTION_GUANG_DETAIL_DATA . strval($id) . $clientType; | 42 | $key = CacheConfig::KEY_ACTION_GUANG_DETAIL_DATA . strval($id) . $clientType; |
43 | 43 | ||
44 | if (USE_CACHE) { | 44 | if (USE_CACHE) { |
45 | - // 先尝试获取二级缓存(slave), 有数据则直接返回. | 45 | + // 先尝试获取一级缓存(master), 有数据则直接返回. |
46 | $cached = Cache::get($key, 'master'); | 46 | $cached = Cache::get($key, 'master'); |
47 | if (!empty($cached)) { | 47 | if (!empty($cached)) { |
48 | $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1000); | 48 | $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false, 1000); |
@@ -56,6 +56,11 @@ class DetailData | @@ -56,6 +56,11 @@ class DetailData | ||
56 | // 获取资讯 | 56 | // 获取资讯 |
57 | $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false); | 57 | $article = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_PACKAGE_ARTICLE, 'getArticle', array($id, $clientType), false); |
58 | if (!isset($article['author_id'])) { | 58 | if (!isset($article['author_id'])) { |
59 | + // 再尝试获取二级缓存(slave), 有数据则直接返回. | ||
60 | + $cached = Cache::get($key, 'slave'); | ||
61 | + if (!empty($cached)) { | ||
62 | + return $cached; | ||
63 | + } | ||
59 | return $result; | 64 | return $result; |
60 | } | 65 | } |
61 | $result['getArticle'] = $article; | 66 | $result['getArticle'] = $article; |
@@ -134,7 +134,7 @@ class PlusstarData | @@ -134,7 +134,7 @@ class PlusstarData | ||
134 | $key = CacheConfig::KEY_ACTION_GUANG_PLUSTAR_DATA . strval($id) . $clientType; | 134 | $key = CacheConfig::KEY_ACTION_GUANG_PLUSTAR_DATA . strval($id) . $clientType; |
135 | 135 | ||
136 | if (USE_CACHE) { | 136 | if (USE_CACHE) { |
137 | - // 先尝试获取二级缓存(slave), 有数据则直接返回. | 137 | + // 先尝试获取一级缓存(master), 有数据则直接返回. |
138 | $cached = Cache::get($key, 'master'); | 138 | $cached = Cache::get($key, 'master'); |
139 | if (!empty($cached)) { | 139 | if (!empty($cached)) { |
140 | // 判断用户是否已收藏 | 140 | // 判断用户是否已收藏 |
@@ -151,6 +151,11 @@ class PlusstarData | @@ -151,6 +151,11 @@ class PlusstarData | ||
151 | // 品牌详情信息 | 151 | // 品牌详情信息 |
152 | $brandInfo = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_PLUSSTAR, 'getBrandInfo', array(array('id' => $id)), 3600); // 缓存1小时 | 152 | $brandInfo = Yohobuy::yarClient(Yohobuy::SERVICE_URL . self::URI_BRANDINFO_PLUSSTAR, 'getBrandInfo', array(array('id' => $id)), 3600); // 缓存1小时 |
153 | if (!isset($brandInfo['data']['brand_id'])) { | 153 | if (!isset($brandInfo['data']['brand_id'])) { |
154 | + // 再尝试获取二级缓存(master), 有数据则直接返回. | ||
155 | + $cached = Cache::get($key, 'slave'); | ||
156 | + if (!empty($cached)) { | ||
157 | + return $cached; | ||
158 | + } | ||
154 | return $result; | 159 | return $result; |
155 | } else { | 160 | } else { |
156 | $result['getBrandInfo'] = $brandInfo; | 161 | $result['getBrandInfo'] = $brandInfo; |
@@ -133,6 +133,15 @@ class IndexModel | @@ -133,6 +133,15 @@ class IndexModel | ||
133 | // 模板中使用JS的标识 | 133 | // 模板中使用JS的标识 |
134 | $data['guangHome'] = true; | 134 | $data['guangHome'] = true; |
135 | 135 | ||
136 | + // 分页需要参数 | ||
137 | + $data['guang']['gender'] = $gender; | ||
138 | + | ||
139 | + // 显示底部 | ||
140 | + $data['pageFooter'] = true; | ||
141 | + | ||
142 | + // 缓存键名 | ||
143 | + $key = CacheConfig::KEY_ACTION_GUANG_LIST_DATA . sprintf('%s_%s_%s', $gender, $type, $page); | ||
144 | + | ||
136 | // 顶部的分类列表 | 145 | // 顶部的分类列表 |
137 | if (!empty($category['data'])) { | 146 | if (!empty($category['data'])) { |
138 | foreach ($category['data'] as $value) { | 147 | foreach ($category['data'] as $value) { |
@@ -157,19 +166,19 @@ class IndexModel | @@ -157,19 +166,19 @@ class IndexModel | ||
157 | $data['guang']['swiper'][] = $build; | 166 | $data['guang']['swiper'][] = $build; |
158 | } | 167 | } |
159 | } | 168 | } |
160 | - // 只有当$type=0时才有广告参数返回, 所以需要再调一次!! | ||
161 | - else { | ||
162 | - $temp = ListData::article('1,3', 0, 0, '', $page); | ||
163 | - if (!empty($temp['data']['list']['adlist'])) { | ||
164 | - foreach ($temp['data']['list']['adlist'] as $value) { | ||
165 | - $build = array(); | ||
166 | - $build['url'] = Helpers::getFilterUrl($value['url']); | ||
167 | - $build['img'] = Helpers::getImageUrl($value['src'], 830, 327); | ||
168 | - $data['guang']['swiper'][] = $build; | ||
169 | - } | ||
170 | - } | ||
171 | - $temp = array(); | ||
172 | - } | 169 | +// // 只有当$type=0时才有广告参数返回, 所以需要再调一次!! |
170 | +// else { | ||
171 | +// $temp = ListData::article('1,3', 0, 0, '', $page); | ||
172 | +// if (!empty($temp['data']['list']['adlist'])) { | ||
173 | +// foreach ($temp['data']['list']['adlist'] as $value) { | ||
174 | +// $build = array(); | ||
175 | +// $build['url'] = Helpers::getFilterUrl($value['url']); | ||
176 | +// $build['img'] = Helpers::getImageUrl($value['src'], 830, 327); | ||
177 | +// $data['guang']['swiper'][] = $build; | ||
178 | +// } | ||
179 | +// } | ||
180 | +// $temp = array(); | ||
181 | +// } | ||
173 | 182 | ||
174 | // 内容列表 | 183 | // 内容列表 |
175 | if (!empty($article['data']['list']['artList'])) { | 184 | if (!empty($article['data']['list']['artList'])) { |
@@ -177,13 +186,22 @@ class IndexModel | @@ -177,13 +186,22 @@ class IndexModel | ||
177 | foreach ($article['data']['list']['artList'] as $value) { | 186 | foreach ($article['data']['list']['artList'] as $value) { |
178 | $data['guang']['infos'][$type]['info'][] = Helpers::formatArticle($value, true, false, true); | 187 | $data['guang']['infos'][$type]['info'][] = Helpers::formatArticle($value, true, false, true); |
179 | } | 188 | } |
180 | - } | ||
181 | 189 | ||
182 | - // 分页需要参数 | ||
183 | - $data['guang']['gender'] = $gender; | ||
184 | - | ||
185 | - // 显示底部 | ||
186 | - $data['pageFooter'] = true; | 190 | + if (USE_CACHE) { |
191 | + $result = Cache::get($key, 'master'); | ||
192 | + if (empty($result)) { | ||
193 | + // 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存 | ||
194 | + Cache::set($key, $data, 3600); | ||
195 | + } | ||
196 | + } | ||
197 | + } | ||
198 | + // 当接口异常时,尝试从二级缓存(slave)获取 | ||
199 | + elseif (USE_CACHE) { | ||
200 | + $result = Cache::get($key, 'slave'); | ||
201 | + if (!empty($result)) { | ||
202 | + return $result; | ||
203 | + } | ||
204 | + } | ||
187 | 205 | ||
188 | $category = array(); | 206 | $category = array(); |
189 | $article = array(); | 207 | $article = array(); |
@@ -40,7 +40,13 @@ class InfoController extends AbstractAction | @@ -40,7 +40,13 @@ class InfoController extends AbstractAction | ||
40 | } | 40 | } |
41 | 41 | ||
42 | // 获取详情内容信息, 异常则跳到错误页面 | 42 | // 获取详情内容信息, 异常则跳到错误页面 |
43 | + $detail = array(); | ||
44 | + try { | ||
43 | $detail = DetailData::package($id, $isApp); | 45 | $detail = DetailData::package($id, $isApp); |
46 | + } catch (\Exception $e) { | ||
47 | + // do nothing | ||
48 | + } | ||
49 | + | ||
44 | if (empty($detail['getArticle'])) { | 50 | if (empty($detail['getArticle'])) { |
45 | $this->_view->display('index', $data); | 51 | $this->_view->display('index', $data); |
46 | return; | 52 | return; |
@@ -113,7 +113,12 @@ class PlusstarController extends AbstractAction | @@ -113,7 +113,12 @@ class PlusstarController extends AbstractAction | ||
113 | } | 113 | } |
114 | 114 | ||
115 | /* 判断品牌信息是否为空 */ | 115 | /* 判断品牌信息是否为空 */ |
116 | + $brandInfo = array(); | ||
117 | + try { | ||
116 | $brandInfo = PlusstarData::brandInfo($id, $gender, $uid, $udid, $isApp); | 118 | $brandInfo = PlusstarData::brandInfo($id, $gender, $uid, $udid, $isApp); |
119 | + } catch (\Exception $e) { | ||
120 | + // do nothing | ||
121 | + } | ||
117 | if (empty($brandInfo['getBrandInfo']['data']['brand_id'])) { | 122 | if (empty($brandInfo['getBrandInfo']['data']['brand_id'])) { |
118 | break; | 123 | break; |
119 | } | 124 | } |
-
Please register or login to post a comment