From 50f27e57d4f4cbe6ad93bebed9eb8285c1dfff2b Mon Sep 17 00:00:00 2001 From: Chopper Date: Mon, 9 Aug 2021 11:46:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E7=BC=93=E5=AD=98=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/goods/service/GoodsService.java | 7 ---- .../goods/serviceimpl/GoodsServiceImpl.java | 35 ++++++++++--------- .../goods/GoodsStoreController.java | 2 +- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java index 340801fd..1853f7c1 100644 --- a/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java +++ b/framework/src/main/java/cn/lili/modules/goods/service/GoodsService.java @@ -58,13 +58,6 @@ public interface GoodsService extends IService { */ GoodsVO getGoodsVO(String goodsId); - /** - * 数据库查询商品VO - * - * @param goodsId 商品id - * @return 商品VO - */ - GoodsVO getGoodsVOFromDB(String goodsId); /** * 商品查询 diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java index 9d57ebcb..7f44dde9 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsServiceImpl.java @@ -4,15 +4,14 @@ import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.json.JSONUtil; +import cn.lili.cache.Cache; +import cn.lili.cache.CachePrefix; import cn.lili.common.enums.ResultCode; import cn.lili.common.exception.ServiceException; -import cn.lili.rocketmq.RocketmqSendCallbackBuilder; -import cn.lili.rocketmq.tags.GoodsTagsEnum; +import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.common.security.context.UserContext; import cn.lili.common.security.enums.UserEnums; -import cn.lili.mybatis.util.PageUtil; import cn.lili.common.utils.StringUtils; -import cn.lili.common.properties.RocketmqCustomProperties; import cn.lili.modules.goods.entity.dos.Category; import cn.lili.modules.goods.entity.dos.Goods; import cn.lili.modules.goods.entity.dos.GoodsGallery; @@ -37,6 +36,9 @@ import cn.lili.modules.system.entity.dos.Setting; import cn.lili.modules.system.entity.dto.GoodsSetting; import cn.lili.modules.system.entity.enums.SettingEnum; import cn.lili.modules.system.service.SettingService; +import cn.lili.mybatis.util.PageUtil; +import cn.lili.rocketmq.RocketmqSendCallbackBuilder; +import cn.lili.rocketmq.tags.GoodsTagsEnum; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; @@ -46,9 +48,6 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.CacheConfig; -import org.springframework.cache.annotation.CachePut; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -64,7 +63,6 @@ import java.util.List; */ @Service @Transactional(rollbackFor = Exception.class) -@CacheConfig(cacheNames = "{goods}") public class GoodsServiceImpl extends ServiceImpl implements GoodsService { @@ -115,6 +113,9 @@ public class GoodsServiceImpl extends ServiceImpl implements private CategoryParameterGroupService categoryParameterGroupService; + @Autowired + private Cache cache; + @Override public void underStoreGoods(String storeId) { //获取商品ID列表 @@ -157,7 +158,6 @@ public class GoodsServiceImpl extends ServiceImpl implements @Override - @CachePut(key = "#goodsId") public void editGoods(GoodsOperationDTO goodsOperationDTO, String goodsId) { Goods goods = new Goods(goodsOperationDTO); goods.setId(goodsId); @@ -177,19 +177,23 @@ public class GoodsServiceImpl extends ServiceImpl implements if (goodsOperationDTO.getGoodsGalleryList() != null && !goodsOperationDTO.getGoodsGalleryList().isEmpty()) { this.goodsGalleryService.add(goodsOperationDTO.getGoodsGalleryList(), goods.getId()); } - + cache.remove(CachePrefix.GOODS.getPrefix() + goodsId); } @Override - @Cacheable(key = "#goodsId") public GoodsVO getGoodsVO(String goodsId) { + //缓存获取,如果没有则读取缓存 + GoodsVO goodsVO = cache.get(CachePrefix.GOODS.getPrefix() + goodsId); + if (goodsVO != null) { + return goodsVO; + } //查询商品信息 Goods goods = this.getById(goodsId); if (goods == null) { log.error("商品ID为" + goodsId + "的商品不存在"); throw new ServiceException(ResultCode.GOODS_NOT_EXIST); } - GoodsVO goodsVO = new GoodsVO(); + goodsVO = new GoodsVO(); //赋值 BeanUtils.copyProperties(goods, goodsVO); //商品id @@ -224,11 +228,6 @@ public class GoodsServiceImpl extends ServiceImpl implements return goodsVO; } - @Override - public GoodsVO getGoodsVOFromDB(String goodsId) { - return getGoodsVO(goodsId); - } - @Override public IPage queryByParams(GoodsSearchParams goodsSearchParams) { return this.page(PageUtil.initPage(goodsSearchParams), goodsSearchParams.queryWrapper()); @@ -242,6 +241,8 @@ public class GoodsServiceImpl extends ServiceImpl implements goods.setIsAuth(goodsAuthEnum.name()); result = this.updateById(goods); goodsSkuService.updateGoodsSkuStatus(goods); + //删除之前的缓存 + cache.remove(CachePrefix.GOODS.getPrefix() + goodsId); //商品审核消息 String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.GOODS_AUDIT.name(); //发送mq消息 diff --git a/seller-api/src/main/java/cn/lili/controller/goods/GoodsStoreController.java b/seller-api/src/main/java/cn/lili/controller/goods/GoodsStoreController.java index 7e4b31cc..2965be06 100644 --- a/seller-api/src/main/java/cn/lili/controller/goods/GoodsStoreController.java +++ b/seller-api/src/main/java/cn/lili/controller/goods/GoodsStoreController.java @@ -94,7 +94,7 @@ public class GoodsStoreController { @GetMapping(value = "/get/{id}") public ResultMessage get(@PathVariable String id) { AuthUser tokenUser = UserContext.getCurrentUser(); - GoodsVO goods = goodsService.getGoodsVOFromDB(id); + GoodsVO goods = goodsService.getGoodsVO(id); assert tokenUser != null; if (tokenUser.getStoreId().equals(goods.getStoreId())) { return ResultUtil.data(goods);