Skip to content

AbstractCacheManager.getCache() breaks contract of CacheManager.getCache() #23193

@Shpota

Description

@Shpota

Affects: Spring Boot version 2.1.3


Problem: According to the documentation, CacheManager.getCache(String) must return

the associated cache, or null if none found

However, AbstractCacheManager doesn't implement this contract. Instead, it always returns a not-null value disregards to what was requested. In fact, it creates a new cache every time an unknown cache is requested.

It is misleading and error-prone. I relied on this functionality in my code and ended up with broken business logic.

More details.
Here is the documentation of CacheManager.getCache(String):

Return the cache associated with the given name.
Params: name – the cache identifier (must not be null)
Returns: the associated cache, or null if none found

And here is the implementation of AbstractCacheManager.getCache(String):

@Override
@Nullable
public Cache getCache(String name) {
    Cache cache = this.cacheMap.get(name);
    if (cache != null) {
        return cache;
    }
    else {
        // Fully synchronize now for missing cache creation...
        synchronized (this.cacheMap) {
            cache = this.cacheMap.get(name);
            if (cache == null) {
                cache = getMissingCache(name);
                if (cache != null) {
                    cache = decorateCache(cache);
                    this.cacheMap.put(name, cache);
                    updateCacheNames(name);
                }
            }
            return cache;
        }
    }
}

Potential solutions: Please either update the documentation and make sure it is mentioned that the method could also create a cache or update the implementation, or, if I am missing something, make the documentation transparent.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions