Commit 3d51c67f1a521f0b4156b1ab2fe327f39ddb9d16
1 parent
92da91a7
拼团活动:app-api 获得拼团活动列表增加 Spring Cache 缓存
Showing
1 changed file
with
24 additions
and
4 deletions
yudao-module-mall/yudao-module-promotion-biz/src/main/java/cn/iocoder/yudao/module/promotion/controller/app/combination/AppCombinationActivityController.java
| ... | ... | @@ -14,6 +14,8 @@ import cn.iocoder.yudao.module.promotion.convert.combination.CombinationActivity |
| 14 | 14 | import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationActivityDO; |
| 15 | 15 | import cn.iocoder.yudao.module.promotion.dal.dataobject.combination.CombinationProductDO; |
| 16 | 16 | import cn.iocoder.yudao.module.promotion.service.combination.CombinationActivityService; |
| 17 | +import com.google.common.cache.CacheLoader; | |
| 18 | +import com.google.common.cache.LoadingCache; | |
| 17 | 19 | import io.swagger.v3.oas.annotations.Operation; |
| 18 | 20 | import io.swagger.v3.oas.annotations.Parameter; |
| 19 | 21 | import io.swagger.v3.oas.annotations.tags.Tag; |
| ... | ... | @@ -24,11 +26,13 @@ import org.springframework.web.bind.annotation.RequestParam; |
| 24 | 26 | import org.springframework.web.bind.annotation.RestController; |
| 25 | 27 | |
| 26 | 28 | import javax.annotation.Resource; |
| 29 | +import java.time.Duration; | |
| 27 | 30 | import java.util.Collections; |
| 28 | 31 | import java.util.List; |
| 29 | 32 | |
| 30 | 33 | import static cn.hutool.core.util.ObjectUtil.defaultIfNull; |
| 31 | 34 | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| 35 | +import static cn.iocoder.yudao.framework.common.util.cache.CacheUtils.buildAsyncReloadingCache; | |
| 32 | 36 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList; |
| 33 | 37 | |
| 34 | 38 | @Tag(name = "用户 APP - 拼团活动") |
| ... | ... | @@ -37,24 +41,40 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils. |
| 37 | 41 | @Validated |
| 38 | 42 | public class AppCombinationActivityController { |
| 39 | 43 | |
| 44 | + /** | |
| 45 | + * {@link AppCombinationActivityRespVO} 缓存,通过它异步刷新 {@link #getCombinationActivityList0(Integer)} 所要的首页数据 | |
| 46 | + */ | |
| 47 | + private final LoadingCache<Integer, List<AppCombinationActivityRespVO>> combinationActivityListCache = buildAsyncReloadingCache(Duration.ofSeconds(10L), | |
| 48 | + new CacheLoader<Integer, List<AppCombinationActivityRespVO>>() { | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + public List<AppCombinationActivityRespVO> load(Integer count) { | |
| 52 | + return getCombinationActivityList0(count); | |
| 53 | + } | |
| 54 | + | |
| 55 | + }); | |
| 56 | + | |
| 40 | 57 | @Resource |
| 41 | 58 | private CombinationActivityService activityService; |
| 42 | 59 | @Resource |
| 43 | 60 | private ProductSpuApi spuApi; |
| 44 | 61 | |
| 45 | - // TODO 芋艿:增加 Spring Cache | |
| 46 | 62 | @GetMapping("/list") |
| 47 | 63 | @Operation(summary = "获得拼团活动列表", description = "用于小程序首页") |
| 48 | 64 | @Parameter(name = "count", description = "需要展示的数量", example = "6") |
| 49 | 65 | public CommonResult<List<AppCombinationActivityRespVO>> getCombinationActivityList( |
| 50 | 66 | @RequestParam(name = "count", defaultValue = "6") Integer count) { |
| 67 | + return success(combinationActivityListCache.getUnchecked(count)); | |
| 68 | + } | |
| 69 | + | |
| 70 | + private List<AppCombinationActivityRespVO> getCombinationActivityList0(Integer count) { | |
| 51 | 71 | List<CombinationActivityDO> list = activityService.getCombinationActivityListByCount(defaultIfNull(count, 6)); |
| 52 | 72 | if (CollUtil.isEmpty(list)) { |
| 53 | - return success(Collections.emptyList()); | |
| 73 | + return Collections.emptyList(); | |
| 54 | 74 | } |
| 55 | 75 | // 拼接返回 |
| 56 | 76 | List<ProductSpuRespDTO> spuList = spuApi.getSpuList(convertList(list, CombinationActivityDO::getSpuId)); |
| 57 | - return success(CombinationActivityConvert.INSTANCE.convertAppList(list, spuList)); | |
| 77 | + return CombinationActivityConvert.INSTANCE.convertAppList(list, spuList); | |
| 58 | 78 | } |
| 59 | 79 | |
| 60 | 80 | @GetMapping("/page") |
| ... | ... | @@ -76,7 +96,7 @@ public class AppCombinationActivityController { |
| 76 | 96 | // 1、获取活动 |
| 77 | 97 | CombinationActivityDO combinationActivity = activityService.getCombinationActivity(id); |
| 78 | 98 | if (combinationActivity == null |
| 79 | - || ObjectUtil.equal(combinationActivity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { | |
| 99 | + || ObjectUtil.equal(combinationActivity.getStatus(), CommonStatusEnum.DISABLE.getStatus())) { | |
| 80 | 100 | return success(null); |
| 81 | 101 | } |
| 82 | 102 | // 2、获取活动商品 | ... | ... |