Fix cache decoration

Prior to this commit, a cache that is added on-the-fly is not properly
decorated by the provided CacheManager implementation that supports
it (EhCache and JCache).

This commits adds an extra getMissingCache method to
the AbstractCacheManager that can be extended to provide a cache that
may exist in the native cache manager but is not yet known by the
spring abstraction.

Issue: SPR-11518
This commit is contained in:
Stephane Nicoll
2014-02-11 11:48:47 +01:00
parent dcf5f4a6a3
commit 119dfd9cf9
6 changed files with 347 additions and 22 deletions

View File

@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
*
* @author Costin Leau
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.1
*/
public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager {
@@ -86,17 +87,14 @@ public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManag
}
@Override
public Cache getCache(String name) {
Cache cache = super.getCache(name);
if (cache == null) {
// Check the EhCache cache again (in case the cache was added at runtime)
Ehcache ehcache = getCacheManager().getEhcache(name);
if (ehcache != null) {
addCache(new EhCacheCache(ehcache));
cache = super.getCache(name); // potentially decorated
}
protected Cache getMissingCache(String name) {
// check the EhCache cache again
// (in case the cache was added at runtime)
Ehcache ehcache = getCacheManager().getEhcache(name);
if (ehcache != null) {
return new EhCacheCache(ehcache);
}
return cache;
return null;
}
}

View File

@@ -31,6 +31,7 @@ import org.springframework.cache.transaction.AbstractTransactionSupportingCacheM
* <p>Note: This class has been updated for JCache 1.0, as of Spring 4.0.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.2
*/
public class JCacheCacheManager extends AbstractTransactionSupportingCacheManager {
@@ -108,17 +109,13 @@ public class JCacheCacheManager extends AbstractTransactionSupportingCacheManage
}
@Override
public Cache getCache(String name) {
Cache cache = super.getCache(name);
if (cache == null) {
// Check the JCache cache again (in case the cache was added at runtime)
javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(name);
if (jcache != null) {
addCache(new JCacheCache(jcache, isAllowNullValues()));
cache = super.getCache(name); // potentially decorated
}
protected Cache getMissingCache(String name) {
// Check the JCache cache again (in case the cache was added at runtime)
javax.cache.Cache<Object, Object> jcache = getCacheManager().getCache(name);
if (jcache != null) {
return new JCacheCache(jcache, isAllowNullValues());
}
return cache;
return null;
}
}