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 c041e523..e23769d8 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 @@ -8,6 +8,9 @@ import cn.lili.modules.goods.entity.enums.GoodsStatusEnum; import cn.lili.modules.goods.entity.vos.GoodsVO; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.IService; +import org.springframework.cache.annotation.CacheConfig; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; import java.util.List; @@ -17,6 +20,7 @@ import java.util.List; * @author pikachu * @date 2020-02-23 16:18:56 */ +@CacheConfig(cacheNames = "{goods}") public interface GoodsService extends IService { @@ -48,6 +52,7 @@ public interface GoodsService extends IService { * @param goodsOperationDTO 商品查询条件 * @param goodsId 商品ID */ + @CacheEvict(key = "#goodsId") void editGoods(GoodsOperationDTO goodsOperationDTO, String goodsId); /** @@ -56,6 +61,7 @@ public interface GoodsService extends IService { * @param goodsId 商品id * @return 商品VO */ + @Cacheable(key = "#goodsId") GoodsVO getGoodsVO(String goodsId); /** diff --git a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java index 0948d00f..0cdd4ac8 100644 --- a/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/goods/serviceimpl/GoodsSkuServiceImpl.java @@ -179,7 +179,9 @@ public class GoodsSkuServiceImpl extends ServiceImpl i @Override public GoodsSku getGoodsSkuByIdFromCache(String id) { + //获取缓存中的sku GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id)); + //如果缓存中没有信息,则查询数据库,然后写入缓存 if (goodsSku == null) { goodsSku = this.getById(id); if (goodsSku == null) { @@ -187,6 +189,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i } cache.put(GoodsSkuService.getCacheKeys(id), goodsSku); } + //获取商品库存 String quantity = stringRedisTemplate.opsForValue().get(GoodsSkuService.getStockCacheKey(id)); if (quantity != null) { if (goodsSku.getQuantity().equals(Convert.toInt(quantity))) {