diff --git a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxHomeController.java b/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxHomeController.java index 7e2d6e60..9493e1a1 100644 --- a/litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxHomeController.java +++ b/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 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 data = new HashMap<>(); - List banner = adService.queryIndex(); - data.put("banner", banner); + Callable bannerListCallable = () -> adService.queryIndex(); - List channel = categoryService.queryChannel(); - data.put("channel", channel); + Callable channelListCallable = () -> categoryService.queryChannel(); - List couponList = couponService.queryList(0, 3); - data.put("couponList", couponList); + Callable couponListCallable = () -> couponService.queryList(0, 3); - List newGoods = goodsService.queryByNew(0, SystemConfig.getNewLimit()); - data.put("newGoodsList", newGoods); + Callable newGoodsListCallable = () -> goodsService.queryByNew(0, SystemConfig.getNewLimit()); - List hotGoods = goodsService.queryByHot(0, SystemConfig.getHotLimit()); - data.put("hotGoodsList", hotGoods); + Callable hotGoodsListCallable = () -> goodsService.queryByHot(0, SystemConfig.getHotLimit()); - List brandList = brandService.queryVO(0, SystemConfig.getBrandLimit()); - data.put("brandList", brandList); + Callable brandListCallable = () -> brandService.queryVO(0, SystemConfig.getBrandLimit()); - List topicList = topicService.queryList(0, SystemConfig.getTopicLimit()); - data.put("topicList", topicList); + Callable topicListCallable = () -> topicService.queryList(0, SystemConfig.getTopicLimit()); //团购专区 - List> grouponList = grouponRulesService.queryList(0, 5); - data.put("grouponList", grouponList); + Callable grouponListCallable = () -> grouponRulesService.queryList(0, 5); + Callable floorGoodsListCallable = this::getCategoryList; + + FutureTask bannerTask = new FutureTask<>(bannerListCallable); + FutureTask channelTask = new FutureTask<>(channelListCallable); + FutureTask couponListTask = new FutureTask<>(couponListCallable); + FutureTask newGoodsListTask = new FutureTask<>(newGoodsListCallable); + FutureTask hotGoodsListTask = new FutureTask<>(hotGoodsListCallable); + FutureTask brandListTask = new FutureTask<>(brandListCallable); + FutureTask topicListTask = new FutureTask<>(topicListCallable); + FutureTask grouponListTask = new FutureTask<>(grouponListCallable); + FutureTask 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 getCategoryList() { List categoryList = new ArrayList<>(); List catL1List = categoryService.queryL1WithoutRecommend(0, SystemConfig.getCatlogListLimit()); for (LitemallCategory catL1 : catL1List) { @@ -102,23 +150,19 @@ public class WxHomeController { l2List.add(catL2.getId()); } - List categoryGoods = null; + List categoryGoods; if (l2List.size() == 0) { categoryGoods = new ArrayList<>(); } else { categoryGoods = goodsService.queryByCategory(l2List, 0, SystemConfig.getCatlogMoreLimit()); } - Map catGoods = new HashMap(); + Map 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; } } \ No newline at end of file