From 394ee27525fa327b4ecdcf0b638a8b3b43cb20db Mon Sep 17 00:00:00 2001 From: Chopper Date: Tue, 2 Nov 2021 12:45:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=93=E5=AD=98=E5=AD=97=E6=AE=B5=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E4=B8=8D=E4=B8=80=E8=87=B4=E9=97=AE=E9=A2=98=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/GoodsSkuServiceImpl.java | 29 ++++++++----------- .../serviceimpl/EsGoodsIndexServiceImpl.java | 2 +- .../cn/lili/test/elasticsearch/EsTest.java | 5 ++-- 3 files changed, 16 insertions(+), 20 deletions(-) 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 ad617615..6fa288a2 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 @@ -64,7 +64,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i * 缓存 */ @Autowired - private Cache cache; + private Cache cache; /** * 分类 */ @@ -75,11 +75,6 @@ public class GoodsSkuServiceImpl extends ServiceImpl i */ @Autowired private GoodsGalleryService goodsGalleryService; - /** - * 缓存 - */ - @Autowired - private StringRedisTemplate stringRedisTemplate; /** * rocketMq */ @@ -195,7 +190,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i @Override public GoodsSku getGoodsSkuByIdFromCache(String id) { //获取缓存中的sku - GoodsSku goodsSku = cache.get(GoodsSkuService.getCacheKeys(id)); + GoodsSku goodsSku = (GoodsSku) cache.get(GoodsSkuService.getCacheKeys(id)); //如果缓存中没有信息,则查询数据库,然后写入缓存 if (goodsSku == null) { goodsSku = this.getById(id); @@ -206,14 +201,14 @@ public class GoodsSkuServiceImpl extends ServiceImpl i } //获取商品库存 - String quantity = stringRedisTemplate.opsForValue().get(GoodsSkuService.getStockCacheKey(id)); + Integer integer = (Integer) cache.get(GoodsSkuService.getStockCacheKey(id)); //库存不为空 - if (StrUtil.isNotEmpty(quantity)) { + if (integer == null) { //库存与缓存中不一致, - if (!goodsSku.getQuantity().equals(Convert.toInt(quantity))) { + if (!goodsSku.getQuantity().equals(integer)) { //写入最新的库存信息 - goodsSku.setQuantity(Convert.toInt(quantity)); + goodsSku.setQuantity(integer); cache.put(GoodsSkuService.getCacheKeys(goodsSku.getId()), goodsSku); } } @@ -408,7 +403,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i goodsSku.setQuantity(quantity); this.update(new LambdaUpdateWrapper().eq(GoodsSku::getId, skuId).set(GoodsSku::getQuantity, quantity)); cache.put(GoodsSkuService.getCacheKeys(skuId), goodsSku); - stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(skuId), quantity.toString()); + cache.put(GoodsSkuService.getStockCacheKey(skuId), quantity); //更新商品库存 List goodsSkus = new ArrayList<>(); @@ -420,12 +415,12 @@ public class GoodsSkuServiceImpl extends ServiceImpl i @Override public Integer getStock(String skuId) { String cacheKeys = GoodsSkuService.getStockCacheKey(skuId); - String stockStr = stringRedisTemplate.opsForValue().get(cacheKeys); - if (stockStr != null) { - return Convert.toInt(stockStr); + Integer stock = (Integer) cache.get(cacheKeys); + if (stock != null) { + return stock; } else { GoodsSku goodsSku = getGoodsSkuByIdFromCache(skuId); - stringRedisTemplate.opsForValue().set(cacheKeys, goodsSku.getQuantity().toString()); + cache.put(cacheKeys, goodsSku.getQuantity()); return goodsSku.getQuantity(); } } @@ -536,7 +531,7 @@ public class GoodsSkuServiceImpl extends ServiceImpl i } goodsSku.setGoodsType(goods.getGoodsType()); skus.add(goodsSku); - stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); + cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity()); } this.saveBatch(skus); return skus; diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index ed5a1ce9..c2cd102c 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -167,7 +167,7 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements Map goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); index.setPromotionMap(goodsCurrentPromotionMap); esGoodsIndices.add(index); - cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); + cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity()); } //初始化商品索引 this.initIndex(esGoodsIndices); diff --git a/manager-api/src/test/java/cn/lili/test/elasticsearch/EsTest.java b/manager-api/src/test/java/cn/lili/test/elasticsearch/EsTest.java index 566a5772..e31c9903 100644 --- a/manager-api/src/test/java/cn/lili/test/elasticsearch/EsTest.java +++ b/manager-api/src/test/java/cn/lili/test/elasticsearch/EsTest.java @@ -2,6 +2,7 @@ package cn.lili.test.elasticsearch; import cn.hutool.core.util.ReflectUtil; import cn.hutool.json.JSONUtil; +import cn.lili.cache.Cache; import cn.lili.common.vo.PageVO; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.entity.enums.GoodsAuthEnum; @@ -52,7 +53,7 @@ class EsTest { private GoodsSkuService goodsSkuService; @Autowired - private StringRedisTemplate stringRedisTemplate; + private Cache cache; @Autowired private PromotionService promotionService; @@ -161,7 +162,7 @@ class EsTest { Map goodsCurrentPromotionMap = promotionService.getGoodsCurrentPromotionMap(index); index.setPromotionMap(goodsCurrentPromotionMap); esGoodsIndices.add(index); - stringRedisTemplate.opsForValue().set(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity().toString()); + cache.put(GoodsSkuService.getStockCacheKey(goodsSku.getId()), goodsSku.getQuantity()); } esGoodsIndexService.initIndex(esGoodsIndices); Assertions.assertTrue(true);