Browse Source

Synchronized cache creation on CacheManager

Issue: SPR-11132
pull/423/head
Juergen Hoeller 12 years ago
parent
commit
de890fd100
  1. 72
      spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

72
spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java vendored

@ -31,9 +31,9 @@ import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory; import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory;
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache; import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
import net.sf.ehcache.event.CacheEventListener; import net.sf.ehcache.event.CacheEventListener;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@ -230,46 +230,48 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea
this.cacheManager = CacheManager.getInstance(); this.cacheManager = CacheManager.getInstance();
} }
// Fetch cache region: If none with the given name exists, create one on the fly. synchronized (this.cacheManager) {
Ehcache rawCache; // Fetch cache region: If none with the given name exists, create one on the fly.
boolean cacheExists = this.cacheManager.cacheExists(cacheName); Ehcache rawCache;
if (cacheExists) { boolean cacheExists = this.cacheManager.cacheExists(cacheName);
if (logger.isDebugEnabled()) { if (cacheExists) {
logger.debug("Using existing EhCache cache region '" + cacheName + "'"); if (logger.isDebugEnabled()) {
logger.debug("Using existing EhCache cache region '" + cacheName + "'");
}
rawCache = this.cacheManager.getEhcache(cacheName);
} }
rawCache = this.cacheManager.getEhcache(cacheName); else {
} if (logger.isDebugEnabled()) {
else { logger.debug("Creating new EhCache cache region '" + cacheName + "'");
if (logger.isDebugEnabled()) { }
logger.debug("Creating new EhCache cache region '" + cacheName + "'"); rawCache = createCache();
rawCache.setBootstrapCacheLoader(this.bootstrapCacheLoader);
} }
rawCache = createCache();
rawCache.setBootstrapCacheLoader(this.bootstrapCacheLoader);
}
if (this.cacheEventListeners != null) { if (this.cacheEventListeners != null) {
for (CacheEventListener listener : this.cacheEventListeners) { for (CacheEventListener listener : this.cacheEventListeners) {
rawCache.getCacheEventNotificationService().registerListener(listener); rawCache.getCacheEventNotificationService().registerListener(listener);
}
}
if (this.statisticsEnabled) {
rawCache.setStatisticsEnabled(true);
}
if (this.sampledStatisticsEnabled) {
rawCache.setSampledStatisticsEnabled(true);
}
if (this.disabled) {
rawCache.setDisabled(true);
} }
}
if (this.statisticsEnabled) {
rawCache.setStatisticsEnabled(true);
}
if (this.sampledStatisticsEnabled) {
rawCache.setSampledStatisticsEnabled(true);
}
if (this.disabled) {
rawCache.setDisabled(true);
}
if (!cacheExists) { if (!cacheExists) {
this.cacheManager.addCache(rawCache); this.cacheManager.addCache(rawCache);
} }
Ehcache decoratedCache = decorateCache(rawCache); Ehcache decoratedCache = decorateCache(rawCache);
if (decoratedCache != rawCache) { if (decoratedCache != rawCache) {
this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache); this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache);
}
this.cache = decoratedCache;
} }
this.cache = decoratedCache;
} }
/** /**

Loading…
Cancel
Save