Skip to content

Commit 747300f

Browse files
committed
added support for most recent EHCache 1.6 configuration properties to EHCacheFactoryBean (SPR-6234)
1 parent 9d59db7 commit 747300f

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

org.springframework.context.support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package org.springframework.cache.ehcache;
1818

1919
import java.io.IOException;
20+
import java.util.Set;
2021

2122
import net.sf.ehcache.Cache;
2223
import net.sf.ehcache.CacheException;
2324
import net.sf.ehcache.CacheManager;
2425
import net.sf.ehcache.Ehcache;
26+
import net.sf.ehcache.bootstrap.BootstrapCacheLoader;
2527
import net.sf.ehcache.constructs.blocking.BlockingCache;
2628
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
2729
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
2830
import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory;
2931
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
32+
import net.sf.ehcache.event.CacheEventListener;
3033
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
3134
import org.apache.commons.logging.Log;
3235
import org.apache.commons.logging.LogFactory;
@@ -49,6 +52,8 @@
4952
* <p>Note: If the named Cache instance is found, the properties will be ignored and the
5053
* Cache instance will be retrieved from the CacheManager.
5154
*
55+
* <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
56+
5257
* @author Dmitriy Kopylenko
5358
* @author Juergen Hoeller
5459
* @since 1.1.1
@@ -72,8 +77,6 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
7277

7378
private boolean overflowToDisk = true;
7479

75-
private String diskStorePath;
76-
7780
private boolean eternal = false;
7881

7982
private int timeToLive = 120;
@@ -84,10 +87,18 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
8487

8588
private int diskExpiryThreadIntervalSeconds = 120;
8689

90+
private int diskSpoolBufferSize = 0;
91+
92+
private boolean clearOnFlush = true;
93+
8794
private boolean blocking = false;
8895

8996
private CacheEntryFactory cacheEntryFactory;
9097

98+
private BootstrapCacheLoader bootstrapCacheLoader;
99+
100+
private Set<CacheEventListener> cacheEventListeners;
101+
91102
private String beanName;
92103

93104
private Ehcache cache;
@@ -134,7 +145,7 @@ public void setMaxElementsOnDisk(int maxElementsOnDisk) {
134145

135146
/**
136147
* Set the memory style eviction policy for this cache.
137-
* Supported values are "LRU", "LFU" and "FIFO", according to the
148+
* <p>Supported values are "LRU", "LFU" and "FIFO", according to the
138149
* constants defined in EHCache's MemoryStoreEvictionPolicy class.
139150
* Default is "LRU".
140151
*/
@@ -160,9 +171,9 @@ public void setEternal(boolean eternal) {
160171
}
161172

162173
/**
163-
* Set t he time in seconds to live for an element before it expires,
174+
* Set the time in seconds to live for an element before it expires,
164175
* i.e. the maximum time between creation time and when an element expires.
165-
* It is only used if the element is not eternal. Default is 120 seconds.
176+
* <p>This is only used if the element is not eternal. Default is 120 seconds.
166177
*/
167178
public void setTimeToLive(int timeToLive) {
168179
this.timeToLive = timeToLive;
@@ -171,28 +182,44 @@ public void setTimeToLive(int timeToLive) {
171182
/**
172183
* Set the time in seconds to idle for an element before it expires, that is,
173184
* the maximum amount of time between accesses before an element expires.
174-
* This is only used if the element is not eternal. Default is 120 seconds.
185+
* <p>This is only used if the element is not eternal. Default is 120 seconds.
175186
*/
176187
public void setTimeToIdle(int timeToIdle) {
177188
this.timeToIdle = timeToIdle;
178189
}
179190

180191
/**
181192
* Set whether the disk store persists between restarts of the Virtual Machine.
182-
* The default is "false".
193+
* Default is "false".
183194
*/
184195
public void setDiskPersistent(boolean diskPersistent) {
185196
this.diskPersistent = diskPersistent;
186197
}
187198

188199
/**
189200
* Set the number of seconds between runs of the disk expiry thread.
190-
* The default is 120 seconds.
201+
* Default is 120 seconds.
191202
*/
192203
public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds) {
193204
this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
194205
}
195206

207+
/**
208+
* Set the amount of memory to allocate the write buffer for puts to the disk store.
209+
* Default is 0.
210+
*/
211+
public void setDiskSpoolBufferSize(int diskSpoolBufferSize) {
212+
this.diskSpoolBufferSize = diskSpoolBufferSize;
213+
}
214+
215+
/**
216+
* Set whether the memory store should be cleared when flush is called on the cache.
217+
* Default is "true".
218+
*/
219+
public void setClearOnFlush(boolean clearOnFlush) {
220+
this.clearOnFlush = clearOnFlush;
221+
}
222+
196223
/**
197224
* Set whether to use a blocking cache that lets read attempts block
198225
* until the requested element is created.
@@ -223,6 +250,22 @@ public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory) {
223250
this.cacheEntryFactory = cacheEntryFactory;
224251
}
225252

253+
/**
254+
* Set an EHCache {@link net.sf.ehcache.bootstrap.BootstrapCacheLoader}
255+
* for this cache, if any.
256+
*/
257+
public void setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader) {
258+
this.bootstrapCacheLoader = bootstrapCacheLoader;
259+
}
260+
261+
/**
262+
* Specify EHCache {@link net.sf.ehcache.event.CacheEventListener cache event listeners}
263+
* to registered with this cache.
264+
*/
265+
public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners) {
266+
this.cacheEventListeners = cacheEventListeners;
267+
}
268+
226269
public void setBeanName(String name) {
227270
this.beanName = name;
228271
}
@@ -244,7 +287,7 @@ public void afterPropertiesSet() throws CacheException, IOException {
244287

245288
// Fetch cache region: If none with the given name exists,
246289
// create one on the fly.
247-
Ehcache rawCache = null;
290+
Ehcache rawCache;
248291
if (this.cacheManager.cacheExists(this.cacheName)) {
249292
if (logger.isDebugEnabled()) {
250293
logger.debug("Using existing EHCache cache region '" + this.cacheName + "'");
@@ -270,11 +313,26 @@ public void afterPropertiesSet() throws CacheException, IOException {
270313
/**
271314
* Create a raw Cache object based on the configuration of this FactoryBean.
272315
*/
273-
private Cache createCache() {
274-
return new Cache(
275-
this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy,
276-
this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle,
277-
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null, null, this.maxElementsOnDisk);
316+
protected Cache createCache() {
317+
// Only call EHCache 1.6 constructor if actually necessary (for compatibility with EHCache 1.3+)
318+
Cache cache = (!this.clearOnFlush) ?
319+
new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy,
320+
this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle,
321+
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null,
322+
this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize,
323+
this.clearOnFlush) :
324+
new Cache(this.cacheName, this.maxElementsInMemory, this.memoryStoreEvictionPolicy,
325+
this.overflowToDisk, null, this.eternal, this.timeToLive, this.timeToIdle,
326+
this.diskPersistent, this.diskExpiryThreadIntervalSeconds, null,
327+
this.bootstrapCacheLoader, this.maxElementsOnDisk, this.diskSpoolBufferSize);
328+
329+
if (this.cacheEventListeners != null) {
330+
for (CacheEventListener listener : this.cacheEventListeners) {
331+
cache.getCacheEventNotificationService().registerListener(listener);
332+
}
333+
}
334+
335+
return cache;
278336
}
279337

280338
/**

org.springframework.context.support/src/main/java/org/springframework/cache/ehcache/EhCacheManagerFactoryBean.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
* and cares for proper shutdown of the CacheManager. EhCacheManagerFactoryBean is
4242
* also necessary for loading EHCache configuration from a non-default config location.
4343
*
44-
* <p>Note: As of Spring 2.0, this FactoryBean will by default create an independent
45-
* CacheManager instance, which requires EHCache 1.2 or higher.
44+
* <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
4645
*
4746
* @author Dmitriy Kopylenko
4847
* @author Juergen Hoeller

0 commit comments

Comments
 (0)