buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

用缓存,你缓存的数据是不是还差点意思?

 

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()...

 

posted on 2022-10-16 02:23  buguge  阅读(44)  评论(0编辑  收藏  举报