zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

缓存数据 意思 是不是 差点
2023-09-14 09:06:26 时间

原始代码

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();
            });
}