用缓存,你缓存的数据是不是还差点意思?
eg.1
原始代码
public String selectLevyInvoiceNameString_Cache(String merId) { List<TMerchantLevyInvoiceTypeVO> merLevyInvoiceTypeList = CacheUtil.getCache("merLevyInvoiceTypeList" + merId, 90, () -> merchantLevyInvoiceTypeDAO.selectList(merId)); if (CollectionUtil.isEmpty(merLevyInvoiceTypeList)) { return ""; } StringBuilder invoiceType = new StringBuilder(); for (TMerchantLevyInvoiceTypeVO tMerchantLevyInvoiceTypeVO : merLevyInvoiceTypeList) { LevyInvoiceClassVO cache = CacheUtil.getCache("levyInvoiceClass" + tMerchantLevyInvoiceTypeVO.getInvoiceTypeId(), 90, () -> { Result<LevyInvoiceClassVO> voResult = levyInvoiceClassService.getById(tMerchantLevyInvoiceTypeVO.getInvoiceTypeId()); if (voResult.isSuccess() && voResult.getResult() != null) { return voResult.getResult(); } return new LevyInvoiceClassVO(); } ); String invoiceTypeName = cache.getBillClassName(); if (StringUtils.isBlank(invoiceTypeName)) { continue; } if (invoiceType.length() > 0) { invoiceType.append("/"); } invoiceType.append(invoiceTypeName); } return invoiceType.toString(); }
重构后:
public String selectLevyInvoiceNameString_Cache(String merId) { return CacheUtil.getCache("merLevyInvoiceTypeList" + merId, 90, () -> { List<TMerchantLevyInvoiceTypeVO> merLevyInvoiceTypeList = merchantLevyInvoiceTypeDAO.selectList(merId); if (CollectionUtil.isEmpty(merLevyInvoiceTypeList)) { return ""; } StringBuilder invoiceType = new StringBuilder(); for (TMerchantLevyInvoiceTypeVO tMerchantLevyInvoiceTypeVO : merLevyInvoiceTypeList) { LevyInvoiceClassVO cache = CacheUtil.getCache("levyInvoiceClass" + tMerchantLevyInvoiceTypeVO.getInvoiceTypeId(), (int) MINUTES.toSeconds(30), () -> { Result<LevyInvoiceClassVO> voResult = levyInvoiceClassService.getById(tMerchantLevyInvoiceTypeVO.getInvoiceTypeId()); if (voResult.isSuccess() && voResult.getResult() != null) { return voResult.getResult(); } return new LevyInvoiceClassVO(); } ); String invoiceTypeName = cache.getBillClassName(); if (StringUtils.isBlank(invoiceTypeName)) { continue; } if (invoiceType.length() > 0) { invoiceType.append("/"); } invoiceType.append(invoiceTypeName); } return invoiceType.toString(); }); }
eg.2
原始代码:
Result<LevyFeeRateDto> feeRateDtoResult = LocalCacheUtil.getCache(cacheKey, () -> { Result<LevyFeeRateDto> levyFeeDtoResult = levyMerchantApi.queryLevyFeeRate(levyId); if (!levyFeeDtoResult.isSuccess() || levyFeeDtoResult.getResult() == null) { return null; } return levyFeeDtoResult; }); if (feeRateDtoResult == null || !feeRateDtoResult.isSuccess()) { throw new RetryOrderException("校验费率错误"); } LevyFeeRateDto feeRateDto = feeRateDtoResult.getResult(); if (feeRateDto.getFeeType()...
重构后:
LevyFeeRateDto feeRateDto = LocalCacheUtil.getCache(cacheKey, () -> { Result<LevyFeeRateDto> levyFeeDtoResult = levyMerchantApi.queryLevyFeeRate(levyId); if (!levyFeeDtoResult.isSuccess() || levyFeeDtoResult.getResult() == null) { return null; } return levyFeeDtoResult.getResult(); }); if (feeRateDto == null) { throw new RetryOrderException("校验费率错误"); } if (feeRateDto.getFeeType()...
当看到一些不好的代码时,会发现我还算优秀;当看到优秀的代码时,也才意识到持续学习的重要!--buguge
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/buguge/p/16795545.html