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吗?那这个方法写的有什么意义呢?求解答

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