Forráskód Böngészése

优化:货拉拉获取城市可选车型信息请求添加缓存机制,缓存三天

shizhongqi 8 hónapja
szülő
commit
a93fb1ef57

+ 4 - 7
common/components/delivery/services/DispatchService.php

@@ -216,12 +216,9 @@ class DispatchService
         }
 
         $cityId = $cities[$formatCity]['city_id'];
-        $cityVehicleList = $this->adapters['huolala']->getCityVehicleList($cityId); // 产生外部请求 -- TODO 优化成不耗时等待
-        $cityInfoRevision = $cityVehicleList['city_info_revision'];
-
-        // 获取所有车型列表
-        $vehicleList = $cityVehicleList['vehicle_list'];
-
+        $cityVehicleList = $this->adapters['huolala']->getCityVehicleList($cityId); // 产生外部请求
+        $cityInfoRevision = $cityVehicleList['city_info_revision']; // 城市版本号
+        $vehicleList = $cityVehicleList['vehicle_list']; // 所有车型列表
         if (empty($vehicleList)) {
             throw new \Exception('没有找到可选车型');
         }
@@ -274,7 +271,7 @@ class DispatchService
                 ],
                 'vehicle_std' => $vehicleStd,
                 'spec_req' => array_values($specReq),
-                'coupon_id' => 123456,
+                'coupon_id' => 123456, // TODO ???
                 'invoice_type' => 1,
                 'order_service_type' => 1,
                 '_meta' => [

+ 49 - 19
common/components/delivery/services/adapter/HuolalaAdapter.php

@@ -1,6 +1,7 @@
 <?php
 namespace common\components\delivery\services\adapter;
 
+use Yii;
 use common\components\delivery\helpers\HttpClient;
 use common\components\delivery\platform\huolala\Huolala;
 
@@ -12,6 +13,8 @@ use common\components\delivery\platform\huolala\Huolala;
  */
 class HuolalaAdapter extends Huolala implements Adapter
 {
+    private const CITY_VEHICLE_CACHE_TTL = 259200; // 3 days
+    private const CITY_VEHICLE_CACHE_KEY_PATTERN = 'huolala-city-vehicle:%d';
     /**
      * 获取已开通城市列表
      * 
@@ -34,19 +37,36 @@ class HuolalaAdapter extends Huolala implements Adapter
      * 通过此接口可获取城市可选车型信息,包括车型详情、计价规则、车型附加要求和额外需求等
      * 
      * @param int $cityId 城市ID
+     * @param bool $forceRefresh 是否强制刷新缓存
      * @return array|null 包含城市车型信息的响应数据
      */
-    public function getCityVehicleList($cityId)
+    public function getCityVehicleList($cityId, $forceRefresh = false)
     {
         // 参数验证
         if (empty($cityId) || !is_numeric($cityId)) {
             return null;
         }
+        $cityId = (int)$cityId;
 
-        // 构建请求参数
-        $payload = $this->buildRequestPayload('u-city-info', ['city_id' => (int)$cityId]);
-        
-        // 发送请求
+        $cacheKey = sprintf(self::CITY_VEHICLE_CACHE_KEY_PATTERN, $cityId);
+        $redis = Yii::$app->has('redis') ? Yii::$app->redis : null;
+
+        if ($redis !== null) {
+            if ($forceRefresh) {
+                $redis->executeCommand('DEL', [$cacheKey]);
+            } else {
+                $cached = $redis->executeCommand('GET', [$cacheKey]);
+                if ($cached !== false && $cached !== null) {
+                    $decoded = json_decode($cached, true);
+                    if (is_array($decoded)) {
+                        return $decoded;
+                    }
+                }
+            }
+        }
+
+        // 构建请求参数,发送请求
+        $payload = $this->buildRequestPayload('u-city-info', ['city_id' => $cityId]);
         $resp = HttpClient::post($this->baseUrl, $payload);
         
         // 处理响应
@@ -63,9 +83,9 @@ class HuolalaAdapter extends Huolala implements Adapter
                         && is_array($vehicle['price_text_item']['exceed_segment_price'])) {
                         foreach ($vehicle['price_text_item']['exceed_segment_price'] as $segment) {
                             $exceedSegmentPrice[] = [
-                                'start_exdistancekm' => (int)($segment['start_exdistancekm'] ?? 0),
-                                'end_exdistancekm' => (int)($segment['end_exdistancekm'] ?? 0),
-                                'price_extra_fen' => (int)($segment['price_extra_fen'] ?? 0),
+                                'start_exdistancekm' => $segment['start_exdistancekm'] ?? 0,
+                                'end_exdistancekm' => $segment['end_exdistancekm'] ?? 0,
+                                'price_extra_fen' => $segment['price_extra_fen'] ?? 0,
                             ];
                         }
                     }
@@ -77,24 +97,24 @@ class HuolalaAdapter extends Huolala implements Adapter
                             $vehicleStdItem[] = [
                                 'name' => $std['name'] ?? '',
                                 'desc' => $std['desc'] ?? '',
-                                'price_fen' => (int)($std['price_fen'] ?? 0),
+                                'price_fen' => $std['price_fen'] ?? 0,
                             ];
                         }
                     }
                     
                     $vehicleList[] = [
-                        'order_vehicle_id' => (int)($vehicle['order_vehicle_id'] ?? 0),
+                        'order_vehicle_id' => $vehicle['order_vehicle_id'] ?? 0,
                         'vehicle_name' => $vehicle['vehicle_name'] ?? '',
                         'img_url_high_light' => $vehicle['img_url_high_light'] ?? '',
                         'vehicle_volume' => $vehicle['vehicle_volume'] ?? '',
                         'vehicle_weight' => $vehicle['vehicle_weight'] ?? '',
                         'vehicle_size' => $vehicle['vehicle_size'] ?? '',
-                        'standard_order_vehicle_id' => (int)($vehicle['standard_order_vehicle_id'] ?? 0),
+                        'standard_order_vehicle_id' => $vehicle['standard_order_vehicle_id'] ?? 0,
                         'text_desc' => $vehicle['text_desc'] ?? '',
-                        'vehicle_attr' => (int)($vehicle['vehicle_attr'] ?? 0),
+                        'vehicle_attr' => $vehicle['vehicle_attr'] ?? 0,
                         'price_text_item' => [
-                            'base_distancekm' => (int)($vehicle['price_text_item']['base_distancekm'] ?? 0),
-                            'base_price_fen' => (int)($vehicle['price_text_item']['base_price_fen'] ?? 0),
+                            'base_distancekm' => $vehicle['price_text_item']['base_distancekm'] ?? 0,
+                            'base_price_fen' => $vehicle['price_text_item']['base_price_fen'] ?? 0,
                             'exceed_segment_price' => $exceedSegmentPrice,
                         ],
                         'vehicle_std_item' => $vehicleStdItem,
@@ -116,14 +136,24 @@ class HuolalaAdapter extends Huolala implements Adapter
                 }
             }
             
-            return [
-                'city_id' => (int)($data['city_id'] ?? 0),
-                'name' => $data['name'] ?? '',
-                'name_en' => $data['name_en'] ?? '',
-                'city_info_revision' => (int)($data['city_info_revision'] ?? 0),
+            $result = [
+                'city_id' => $data['city_id'],
+                'name' => $data['name'],
+                'name_en' => $data['name_en'],
+                'city_info_revision' => $data['city_info_revision'],
                 'vehicle_list' => $vehicleList,
                 'spec_req_item' => $specReqItem,
             ];
+            
+            if ($redis !== null) {
+                $redis->executeCommand('SETEX', [
+                    $cacheKey,
+                    self::CITY_VEHICLE_CACHE_TTL,
+                    json_encode($result, JSON_UNESCAPED_UNICODE)
+                ]);
+            }
+
+            return $result;
         }
         
         return null;