就是一个小屁孩 2019-12-02 17:26 采纳率: 0%
浏览 727

AbstractCacheManager中getCache的不解。

SpringBoot中**AbstractCacheManager**这个抽象类中,有一个方法我比较纳闷。

@Override
    @Nullable
    public Cache getCache(String name) {
        // Quick check for existing cache...
        Cache cache = this.cacheMap.get(name);
        if (cache != null) {
            return cache;
        }

        // The provider may support on-demand cache creation...
        Cache missingCache = getMissingCache(name);
        if (missingCache != null) {
            // Fully synchronize now for missing cache registration
            synchronized (this.cacheMap) {
                cache = this.cacheMap.get(name);
                if (cache == null) {
                    cache = decorateCache(missingCache);
                    this.cacheMap.put(name, cache);
                    updateCacheNames(name);
                }
            }
        }
        return cache;
    }

Cache missingCache = getMissingCache(name);也就是这一条。在这个类的最后,写了这个getMissingCache()方法。这个方法如论如何都是返回一个null。

@Nullable
    protected Cache getMissingCache(String name) {
        return null;
    }

而在getCache方法中,又要进行判断是否为空。

if (missingCache != null) {
            // Fully synchronize now for missing cache registration
            synchronized (this.cacheMap) {
                cache = this.cacheMap.get(name);
                if (cache == null) {
                    cache = decorateCache(missingCache);
                    this.cacheMap.put(name, cache);
                    updateCacheNames(name);
                }
            }
        }

这不是无论如何都是null吗?那这个方法写的有什么意义呢?求解答

  • 写回答

1条回答 默认 最新

  • qq_40751218 2019-12-02 17:43
    关注

    AbstractCacheManager 是一个抽象类,具体在调用的时候需要创建实例,如RedisCacheManager 为其子类,子类会重写这个方法:protected RedisCache getMissingCache(String name) {
    return this.allowInFlightCacheCreation ? this.createRedisCache(name, this.defaultCacheConfig) : null;
    }, 这也是一个抽象模板模式

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器