优化代码:获取商品运费模板信息

List<FreightPriceDto> freightPriceDtoList = new LinkedList<>();
Map<Long, ProductVo> productInfoMap = cache.mget(productIds.stream().map(id -> PRODUCT_DETAIL_CACHE + id).collect(Collectors.toList()));
List<Long> notInCacheIds = new ArrayList<>();
for (Long productId : productIds) {
    ProductVo productInfo = productInfoMap.get(PRODUCT_DETAIL_CACHE + productId);
    if (productInfo == null || productInfo.getProductInfo() == null || productInfo.getProductInfo().getTempId() == null) {
        notInCacheIds.add(productId);
    }
}
if (!notInCacheIds.isEmpty()) {
    List<ApiResult<ProductVo>> productVoApiResults = notInCacheIds.stream()
            .map(id -> iProductApi.productDetail(id.toString(), null))
            .collect(Collectors.toList());
    productVoApiResults.forEach(result -> {
        if (result.isSuccess()) {
            ProductVo productInfo = result.getData();
            cache.set(PRODUCT_DETAIL_CACHE + productInfo.getProductInfo().getId(), productInfo, PRODUCT_DETAIL_CACHE_EXPIRE);
        }
    });
}
for (Long productId : productIds) {
    FreightPriceDto freightPriceDto = new FreightPriceDto();
    //获取商品的运费模板id
    ProductVo productInfo = productInfoMap.get(PRODUCT_DETAIL_CACHE + productId);
    Integer tempId = productInfo.getProductInfo().getTempId();
    //查询模板不可配送的区域
    List<RegionInfoDto> regionInfoDto = productUnDelivery(tempId);
    //当前运费模板配送规则
    String key = TEMPLATE_REGION + tempId;
    List<YxShippingTemplatesRegion> yxShippingTemplatesRegion = templateRegionListLocalCache.get(key);
    if (CollectionUtil.isEmpty(yxShippingTemplatesRegion)) {
        yxShippingTemplatesRegion = CompletableFuture.supplyAsync(() -> {
            LambdaQueryWrapper<YxShippingTemplatesRegion> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YxShippingTemplatesRegion::getTempId, tempId);
            return yxShippingTemplatesRegionMapper.selectList(wrapper);
        }).join();
        templateRegionListLocalCache.setLocalCache(key, yxShippingTemplatesRegion);
    }
    freightPriceDto.setProductId(productId);
    freightPriceDto.setTemplatesRegions(yxShippingTemplatesRegion);
    freightPriceDto.setRegionInfo(regionInfoDto);
    freightPriceDtoList.add(freightPriceDto);
}
return freightPriceDtoList;

优化建议:

  1. 使用批量查询代替循环单个查询:可以将所有商品的id集合传入一次性查询,减少循环查询的次数,提高效率。

  2. 使用缓存代替单个查询:可以将商品详情信息缓存起来,下次查询时直接从缓存中获取,减少数据库查询的次数,提高效率。

  3. 使用并发处理优化代码:可以使用并发框架如CompletableFuture,将多个查询操作并发执行,提高效率。

  4. 优化缓存读写操作:可以使用本地缓存代替远程缓存,减少网络传输延迟,提高效率。

优化后代码如下:

List<FreightPriceDto> freightPriceDtoList = new LinkedList<>();
Map<Long, ProductVo> productInfoMap = cache.mget(productIds.stream().map(id -> PRODUCT_DETAIL_CACHE + id).collect(Collectors.toList()));
List<Long> notInCacheIds = new ArrayList<>();
for (Long productId : productIds) {
    ProductVo productInfo = productInfoMap.get(PRODUCT_DETAIL_CACHE + productId);
    if (productInfo == null || productInfo.getProductInfo() == null || productInfo.getProductInfo().getTempId() == null) {
        notInCacheIds.add(productId);
    }
}
if (!notInCacheIds.isEmpty()) {
    List<ApiResult<ProductVo>> productVoApiResults = notInCacheIds.stream()
            .map(id -> iProductApi.productDetail(id.toString(), null))
            .collect(Collectors.toList());
    productVoApiResults.forEach(result -> {
        if (result.isSuccess()) {
            ProductVo productInfo = result.getData();
            cache.set(PRODUCT_DETAIL_CACHE + productInfo.getProductInfo().getId(), productInfo, PRODUCT_DETAIL_CACHE_EXPIRE);
        }
    });
}
for (Long productId : productIds) {
    FreightPriceDto freightPriceDto = new FreightPriceDto();
    //获取商品的运费模板id
    ProductVo productInfo = productInfoMap.get(PRODUCT_DETAIL_CACHE + productId);
    Integer tempId = productInfo.getProductInfo().getTempId();
    //查询模板不可配送的区域
    List<RegionInfoDto> regionInfoDto = productUnDelivery(tempId);
    //当前运费模板配送规则
    String key = TEMPLATE_REGION + tempId;
    List<YxShippingTemplatesRegion> yxShippingTemplatesRegion = templateRegionListLocalCache.get(key);
    if (CollectionUtil.isEmpty(yxShippingTemplatesRegion)) {
        yxShippingTemplatesRegion = CompletableFuture.supplyAsync(() -> {
            LambdaQueryWrapper<YxShippingTemplatesRegion> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(YxShippingTemplatesRegion::getTempId, tempId);
            return yxShippingTemplatesRegionMapper.selectList(wrapper);
        }).join();
        templateRegionListLocalCache.setLocalCache(key, yxShippingTemplatesRegion);
    }
    freightPriceDto.setProductId(productId);
    freightPriceDto.setTemplatesRegions(yxShippingTemplatesRegion);
    freightPriceDto.setRegionInfo(regionInfoDto);
    freightPriceDtoList.add(freightPriceDto);
}
return freightPriceDtoList;

说明:

  • cache.mget 用于批量获取缓存中的商品详情信息。
  • notInCacheIds 用于存储缓存中不存在的商品id。
  • 使用 CompletableFuture 并发获取运费模板配送规则。
  • 使用本地缓存 templateRegionListLocalCache 存储运费模板配送规则,减少网络请求次数。

代码优化后,可以提高代码效率,减少数据库查询次数和网络请求次数。

优化代码:获取商品运费模板信息

原文地址: https://www.cveoy.top/t/topic/ovnA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录