首页 > 其他分享 >用缓存,你缓存对了吗?

用缓存,你缓存对了吗?

时间:2022-10-16 02:33:05浏览次数:42  
标签:voResult 缓存 tMerchantLevyInvoiceTypeVO invoiceType return merLevyInvoiceTypeList

原始代码

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

 

标签:voResult,缓存,tMerchantLevyInvoiceTypeVO,invoiceType,return,merLevyInvoiceTypeList
From: https://www.cnblogs.com/buguge/p/16795545.html

相关文章

  • HMdubbo7【dubbo高级:地址缓存】
    12......
  • 【源码级】MyBatis缓存策略(一级和二级缓存)
    缓存就是内存中的数据,常常来自对数据库查询结果的保存。使用缓存,我们可以避免频繁的与数据库进行交互,进而提高响应速度MyBatis也提供了对缓存的支持,分为一级缓存和二级缓存,......
  • 缓存穿透和雪崩
    缓存穿透缓存穿透的概念很简单,用户想要查询一个数据,发现redis内存数据库没有,也就是缓存没有命中,于是向持久层数据库查询。发现也没有,于是本次查询失败。当用户很多的时候......
  • leetcode 146. LRU Cache LRU 缓存 (简单)
    一、题目大意请你设计并实现一个满足LRU(最近最少使用)缓存约束的数据结构。实现LRUCache类:LRUCache(intcapacity)以正整数作为容量capacity初始化LRU......
  • 【源码级】MyBatis缓存策略
    缓存就是内存中的数据,常常来自对数据库查询结果的保存。使用缓存,我们可以避免频繁的与数据库进行交互,进而提高响应速度MyBatis也提供了对缓存的支持,分为一级缓存和二级缓存......
  • 浏览器缓存
    1.什么是浏览器缓存?浏览器和服务器的通信方式是应答模式。浏览器想得到数据,需要发送http请求给服务器,服务器响应该请求并返回结果。如果每一次浏览器都重新去请求服务器,那......
  • 如何保证缓存与数据库的双写一致性?
    一般来说,如果允许缓存可以稍微的跟数据库偶尔有不一致的情况,也就是说如果你的系统不是严格要求“缓存+数据库”必须保持一致性的话,最好不要做这个方案,即:读请求和写请求......
  • 追求性能极致:客户端缓存带来的革命
    背景2020年5月份,Redis官方推出了令人瞩目的Redis6.0,提出很多新特性,包括了客户端缓存(Clientsidecaching)、ACL、ThreadedI/O和RedisClusterProxy等诸多新特性。......
  • 有关Idea的Invalidate Caches/Restart (Idea本地缓存异常,导致Idea无法识别Maven依赖)
    分析原因:有时我们自己在编写项目后,再次打开会发现自己封装的类,导入后还是爆红。这是因为Idea本地缓存异常,导致Idea无法识别Maven依赖,从而无法找到某些类。解决方式:通过Idea......
  • 缓存概念介绍
     一、缓存是什么?    缓存其实是内存中一个以key-value(键值对)的形式存储数据的一个空间。当然,缓存也可以持久化。常用的缓存技术:Memcached、Ehcache、OsCache、R......