提交 747300f3 编写于 作者: J Juergen Hoeller

added support for most recent EHCache 1.6 configuration properties to EHCacheFactoryBean (SPR-6234)

上级 9d59db79
...@@ -17,16 +17,19 @@ ...@@ -17,16 +17,19 @@
package org.springframework.cache.ehcache; package org.springframework.cache.ehcache;
import java.io.IOException; import java.io.IOException;
import java.util.Set;
import net.sf.ehcache.Cache; import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException; import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager; import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache; import net.sf.ehcache.Ehcache;
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
import net.sf.ehcache.constructs.blocking.BlockingCache; import net.sf.ehcache.constructs.blocking.BlockingCache;
import net.sf.ehcache.constructs.blocking.CacheEntryFactory; import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache; 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.store.MemoryStoreEvictionPolicy; import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
...@@ -49,6 +52,8 @@ import org.springframework.util.Assert; ...@@ -49,6 +52,8 @@ import org.springframework.util.Assert;
* <p>Note: If the named Cache instance is found, the properties will be ignored and the * <p>Note: If the named Cache instance is found, the properties will be ignored and the
* Cache instance will be retrieved from the CacheManager. * Cache instance will be retrieved from the CacheManager.
* *
* <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
* @author Dmitriy Kopylenko * @author Dmitriy Kopylenko
* @author Juergen Hoeller * @author Juergen Hoeller
* @since 1.1.1 * @since 1.1.1
...@@ -72,8 +77,6 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -72,8 +77,6 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
private boolean overflowToDisk = true; private boolean overflowToDisk = true;
private String diskStorePath;
private boolean eternal = false; private boolean eternal = false;
private int timeToLive = 120; private int timeToLive = 120;
...@@ -84,10 +87,18 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -84,10 +87,18 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
private int diskExpiryThreadIntervalSeconds = 120; private int diskExpiryThreadIntervalSeconds = 120;
private int diskSpoolBufferSize = 0;
private boolean clearOnFlush = true;
private boolean blocking = false; private boolean blocking = false;
private CacheEntryFactory cacheEntryFactory; private CacheEntryFactory cacheEntryFactory;
private BootstrapCacheLoader bootstrapCacheLoader;
private Set<CacheEventListener> cacheEventListeners;
private String beanName; private String beanName;
private Ehcache cache; private Ehcache cache;
...@@ -134,7 +145,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -134,7 +145,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set the memory style eviction policy for this cache. * Set the memory style eviction policy for this cache.
* Supported values are "LRU", "LFU" and "FIFO", according to the * <p>Supported values are "LRU", "LFU" and "FIFO", according to the
* constants defined in EHCache's MemoryStoreEvictionPolicy class. * constants defined in EHCache's MemoryStoreEvictionPolicy class.
* Default is "LRU". * Default is "LRU".
*/ */
...@@ -160,9 +171,9 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -160,9 +171,9 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
} }
/** /**
* Set t he time in seconds to live for an element before it expires, * Set the time in seconds to live for an element before it expires,
* i.e. the maximum time between creation time and when an element expires. * i.e. the maximum time between creation time and when an element expires.
* It is only used if the element is not eternal. Default is 120 seconds. * <p>This is only used if the element is not eternal. Default is 120 seconds.
*/ */
public void setTimeToLive(int timeToLive) { public void setTimeToLive(int timeToLive) {
this.timeToLive = timeToLive; this.timeToLive = timeToLive;
...@@ -171,7 +182,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -171,7 +182,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set the time in seconds to idle for an element before it expires, that is, * Set the time in seconds to idle for an element before it expires, that is,
* the maximum amount of time between accesses before an element expires. * the maximum amount of time between accesses before an element expires.
* This is only used if the element is not eternal. Default is 120 seconds. * <p>This is only used if the element is not eternal. Default is 120 seconds.
*/ */
public void setTimeToIdle(int timeToIdle) { public void setTimeToIdle(int timeToIdle) {
this.timeToIdle = timeToIdle; this.timeToIdle = timeToIdle;
...@@ -179,7 +190,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -179,7 +190,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set whether the disk store persists between restarts of the Virtual Machine. * Set whether the disk store persists between restarts of the Virtual Machine.
* The default is "false". * Default is "false".
*/ */
public void setDiskPersistent(boolean diskPersistent) { public void setDiskPersistent(boolean diskPersistent) {
this.diskPersistent = diskPersistent; this.diskPersistent = diskPersistent;
...@@ -187,12 +198,28 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -187,12 +198,28 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set the number of seconds between runs of the disk expiry thread. * Set the number of seconds between runs of the disk expiry thread.
* The default is 120 seconds. * Default is 120 seconds.
*/ */
public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds) { public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds) {
this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds; this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
} }
/**
* Set the amount of memory to allocate the write buffer for puts to the disk store.
* Default is 0.
*/
public void setDiskSpoolBufferSize(int diskSpoolBufferSize) {
this.diskSpoolBufferSize = diskSpoolBufferSize;
}
/**
* Set whether the memory store should be cleared when flush is called on the cache.
* Default is "true".
*/
public void setClearOnFlush(boolean clearOnFlush) {
this.clearOnFlush = clearOnFlush;
}
/** /**
* Set whether to use a blocking cache that lets read attempts block * Set whether to use a blocking cache that lets read attempts block
* until the requested element is created. * until the requested element is created.
...@@ -223,6 +250,22 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -223,6 +250,22 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
this.cacheEntryFactory = cacheEntryFactory; this.cacheEntryFactory = cacheEntryFactory;
} }
/**
* Set an EHCache {@link net.sf.ehcache.bootstrap.BootstrapCacheLoader}
* for this cache, if any.
*/
public void setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader) {
this.bootstrapCacheLoader = bootstrapCacheLoader;
}
/**
* Specify EHCache {@link net.sf.ehcache.event.CacheEventListener cache event listeners}
* to registered with this cache.
*/
public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners) {
this.cacheEventListeners = cacheEventListeners;
}
public void setBeanName(String name) { public void setBeanName(String name) {
this.beanName = name; this.beanName = name;
} }
...@@ -244,7 +287,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -244,7 +287,7 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
// Fetch cache region: If none with the given name exists, // Fetch cache region: If none with the given name exists,
// create one on the fly. // create one on the fly.
Ehcache rawCache = null; Ehcache rawCache;
if (this.cacheManager.cacheExists(this.cacheName)) { if (this.cacheManager.cacheExists(this.cacheName)) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Using existing EHCache cache region '" + this.cacheName + "'"); logger.debug("Using existing EHCache cache region '" + this.cacheName + "'");
...@@ -270,11 +313,26 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, ...@@ -270,11 +313,26 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Create a raw Cache object based on the configuration of this FactoryBean. * Create a raw Cache object based on the configuration of this FactoryBean.
*/ */
private Cache createCache() { protected Cache createCache() {
return new Cache( // Only call EHCache 1.6 constructor if actually necessary (for compatibility with EHCache 1.3+)
this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy, Cache cache = (!this.clearOnFlush) ?
this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle, new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy,
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null, null, this.maxElementsOnDisk); this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle,
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null,
this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize,
this.clearOnFlush) :
new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy,
this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle,
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null,
this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize);
if (this.cacheEventListeners != null) {
for (CacheEventListener listener : this.cacheEventListeners) {
cache.getCacheEventNotificationService().registerListener(listener);
}
}
return cache;
} }
/** /**
......
...@@ -41,8 +41,7 @@ import org.springframework.core.io.Resource; ...@@ -41,8 +41,7 @@ import org.springframework.core.io.Resource;
* and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is * and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is
* also necessary for loading EHCache configuration from a non-default config location. * also necessary for loading EHCache configuration from a non-default config location.
* *
* <p>Note: As of Spring 2.0, this FactoryBean will by default create an independent * <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
* CacheManager instance, which requires EHCache 1.2 or higher.
* *
* @author Dmitriy Kopylenko * @author Dmitriy Kopylenko
* @author Juergen Hoeller * @author Juergen Hoeller
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册