就是一个小屁孩 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;
    }, 这也是一个抽象模板模式

    评论

报告相同问题?

悬赏问题

  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信