Browse Source

首页优化查询方式

Hong 7 years ago
parent
commit
cb496119af

+ 69 - 25
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxHomeController.java

@@ -4,7 +4,8 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.linlinjava.litemall.core.system.SystemConfig;
 import org.linlinjava.litemall.core.util.ResponseUtil;
-import org.linlinjava.litemall.db.domain.*;
+import org.linlinjava.litemall.db.domain.LitemallCategory;
+import org.linlinjava.litemall.db.domain.LitemallGoods;
 import org.linlinjava.litemall.db.service.*;
 import org.linlinjava.litemall.wx.service.HomeCacheManager;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -18,6 +19,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.*;
 
 /**
  * 首页服务
@@ -26,22 +28,35 @@ import java.util.Map;
 @RequestMapping("/wx/home")
 @Validated
 public class WxHomeController {
+    private final Log logger = LogFactory.getLog(WxHomeController.class);
 
     @Autowired
     private LitemallAdService adService;
+
     @Autowired
     private LitemallGoodsService goodsService;
+
     @Autowired
     private LitemallBrandService brandService;
+
     @Autowired
     private LitemallTopicService topicService;
+
     @Autowired
     private LitemallCategoryService categoryService;
+
     @Autowired
     private LitemallGrouponRulesService grouponRulesService;
+
     @Autowired
     private LitemallCouponService couponService;
 
+    private final static ArrayBlockingQueue<Runnable> WORK_QUEUE = new ArrayBlockingQueue<>(9);
+
+    private final static RejectedExecutionHandler HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();
+
+    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(9, 9, 1000, TimeUnit.MILLISECONDS, WORK_QUEUE, HANDLER);
+
     @GetMapping("/cache")
     public Object cache(@NotNull String key) {
         if (!key.equals("litemall_cache")) {
@@ -65,34 +80,67 @@ public class WxHomeController {
             return ResponseUtil.ok(HomeCacheManager.getCacheData(HomeCacheManager.INDEX));
         }
 
-
         Map<String, Object> data = new HashMap<>();
 
-        List<LitemallAd> banner = adService.queryIndex();
-        data.put("banner", banner);
+        Callable<List> bannerListCallable = () -> adService.queryIndex();
 
-        List<LitemallCategory> channel = categoryService.queryChannel();
-        data.put("channel", channel);
+        Callable<List> channelListCallable = () -> categoryService.queryChannel();
 
-        List<LitemallCoupon> couponList = couponService.queryList(0, 3);
-        data.put("couponList", couponList);
+        Callable<List> couponListCallable = () -> couponService.queryList(0, 3);
 
-        List<LitemallGoods> newGoods = goodsService.queryByNew(0, SystemConfig.getNewLimit());
-        data.put("newGoodsList", newGoods);
+        Callable<List> newGoodsListCallable = () -> goodsService.queryByNew(0, SystemConfig.getNewLimit());
 
-        List<LitemallGoods> hotGoods = goodsService.queryByHot(0, SystemConfig.getHotLimit());
-        data.put("hotGoodsList", hotGoods);
+        Callable<List> hotGoodsListCallable = () -> goodsService.queryByHot(0, SystemConfig.getHotLimit());
 
-        List<LitemallBrand> brandList = brandService.queryVO(0, SystemConfig.getBrandLimit());
-        data.put("brandList", brandList);
+        Callable<List> brandListCallable = () -> brandService.queryVO(0, SystemConfig.getBrandLimit());
 
-        List<LitemallTopic> topicList = topicService.queryList(0, SystemConfig.getTopicLimit());
-        data.put("topicList", topicList);
+        Callable<List> topicListCallable = () -> topicService.queryList(0, SystemConfig.getTopicLimit());
 
         //团购专区
-        List<Map<String, Object>> grouponList = grouponRulesService.queryList(0, 5);
-        data.put("grouponList", grouponList);
+        Callable<List> grouponListCallable = () -> grouponRulesService.queryList(0, 5);
+
+        Callable<List> floorGoodsListCallable = this::getCategoryList;
+
+        FutureTask<List> bannerTask = new FutureTask<>(bannerListCallable);
+        FutureTask<List> channelTask = new FutureTask<>(channelListCallable);
+        FutureTask<List> couponListTask = new FutureTask<>(couponListCallable);
+        FutureTask<List> newGoodsListTask = new FutureTask<>(newGoodsListCallable);
+        FutureTask<List> hotGoodsListTask = new FutureTask<>(hotGoodsListCallable);
+        FutureTask<List> brandListTask = new FutureTask<>(brandListCallable);
+        FutureTask<List> topicListTask = new FutureTask<>(topicListCallable);
+        FutureTask<List> grouponListTask = new FutureTask<>(grouponListCallable);
+        FutureTask<List> floorGoodsListTask = new FutureTask<>(floorGoodsListCallable);
+
+        executorService.submit(bannerTask);
+        executorService.submit(channelTask);
+        executorService.submit(couponListTask);
+        executorService.submit(newGoodsListTask);
+        executorService.submit(hotGoodsListTask);
+        executorService.submit(brandListTask);
+        executorService.submit(topicListTask);
+        executorService.submit(grouponListTask);
+        executorService.submit(floorGoodsListTask);
+
+        try {
+            data.put("banner", bannerTask.get());
+            data.put("channel", channelTask.get());
+            data.put("couponList", couponListTask.get());
+            data.put("newGoodsList", newGoodsListTask.get());
+            data.put("hotGoodsList", hotGoodsListTask.get());
+            data.put("brandList", brandListTask.get());
+            data.put("topicList", topicListTask.get());
+            data.put("grouponList", grouponListTask.get());
+            data.put("floorGoodsList", floorGoodsListTask);
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+        }
+        //缓存数据
+        HomeCacheManager.loadData(HomeCacheManager.INDEX, data);
+        return ResponseUtil.ok(data);
+    }
 
+    private List<Map> getCategoryList() {
         List<Map> categoryList = new ArrayList<>();
         List<LitemallCategory> catL1List = categoryService.queryL1WithoutRecommend(0, SystemConfig.getCatlogListLimit());
         for (LitemallCategory catL1 : catL1List) {
@@ -102,23 +150,19 @@ public class WxHomeController {
                 l2List.add(catL2.getId());
             }
 
-            List<LitemallGoods> categoryGoods = null;
+            List<LitemallGoods> categoryGoods;
             if (l2List.size() == 0) {
                 categoryGoods = new ArrayList<>();
             } else {
                 categoryGoods = goodsService.queryByCategory(l2List, 0, SystemConfig.getCatlogMoreLimit());
             }
 
-            Map<String, Object> catGoods = new HashMap<String, Object>();
+            Map<String, Object> catGoods = new HashMap<>();
             catGoods.put("id", catL1.getId());
             catGoods.put("name", catL1.getName());
             catGoods.put("goodsList", categoryGoods);
             categoryList.add(catGoods);
         }
-        data.put("floorGoodsList", categoryList);
-
-        //缓存数据
-        HomeCacheManager.loadData(HomeCacheManager.INDEX, data);
-        return ResponseUtil.ok(data);
+        return categoryList;
     }
 }