17
17
package org .springframework .cache .ehcache ;
18
18
19
19
import java .io .IOException ;
20
+ import java .util .Set ;
20
21
21
22
import net .sf .ehcache .Cache ;
22
23
import net .sf .ehcache .CacheException ;
23
24
import net .sf .ehcache .CacheManager ;
24
25
import net .sf .ehcache .Ehcache ;
26
+ import net .sf .ehcache .bootstrap .BootstrapCacheLoader ;
25
27
import net .sf .ehcache .constructs .blocking .BlockingCache ;
26
28
import net .sf .ehcache .constructs .blocking .CacheEntryFactory ;
27
29
import net .sf .ehcache .constructs .blocking .SelfPopulatingCache ;
28
30
import net .sf .ehcache .constructs .blocking .UpdatingCacheEntryFactory ;
29
31
import net .sf .ehcache .constructs .blocking .UpdatingSelfPopulatingCache ;
32
+ import net .sf .ehcache .event .CacheEventListener ;
30
33
import net .sf .ehcache .store .MemoryStoreEvictionPolicy ;
31
34
import org .apache .commons .logging .Log ;
32
35
import org .apache .commons .logging .LogFactory ;
49
52
* <p>Note: If the named Cache instance is found, the properties will be ignored and the
50
53
* Cache instance will be retrieved from the CacheManager.
51
54
*
55
+ * <p>Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
56
+
52
57
* @author Dmitriy Kopylenko
53
58
* @author Juergen Hoeller
54
59
* @since 1.1.1
@@ -72,8 +77,6 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
72
77
73
78
private boolean overflowToDisk = true ;
74
79
75
- private String diskStorePath ;
76
-
77
80
private boolean eternal = false ;
78
81
79
82
private int timeToLive = 120 ;
@@ -84,10 +87,18 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
84
87
85
88
private int diskExpiryThreadIntervalSeconds = 120 ;
86
89
90
+ private int diskSpoolBufferSize = 0 ;
91
+
92
+ private boolean clearOnFlush = true ;
93
+
87
94
private boolean blocking = false ;
88
95
89
96
private CacheEntryFactory cacheEntryFactory ;
90
97
98
+ private BootstrapCacheLoader bootstrapCacheLoader ;
99
+
100
+ private Set <CacheEventListener > cacheEventListeners ;
101
+
91
102
private String beanName ;
92
103
93
104
private Ehcache cache ;
@@ -134,7 +145,7 @@ public void setMaxElementsOnDisk(int maxElementsOnDisk) {
134
145
135
146
/**
136
147
* 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
138
149
* constants defined in EHCache's MemoryStoreEvictionPolicy class.
139
150
* Default is "LRU".
140
151
*/
@@ -160,9 +171,9 @@ public void setEternal(boolean eternal) {
160
171
}
161
172
162
173
/**
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,
164
175
* 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.
166
177
*/
167
178
public void setTimeToLive (int timeToLive ) {
168
179
this .timeToLive = timeToLive ;
@@ -171,28 +182,44 @@ public void setTimeToLive(int timeToLive) {
171
182
/**
172
183
* Set the time in seconds to idle for an element before it expires, that is,
173
184
* 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.
175
186
*/
176
187
public void setTimeToIdle (int timeToIdle ) {
177
188
this .timeToIdle = timeToIdle ;
178
189
}
179
190
180
191
/**
181
192
* Set whether the disk store persists between restarts of the Virtual Machine.
182
- * The default is "false".
193
+ * Default is "false".
183
194
*/
184
195
public void setDiskPersistent (boolean diskPersistent ) {
185
196
this .diskPersistent = diskPersistent ;
186
197
}
187
198
188
199
/**
189
200
* Set the number of seconds between runs of the disk expiry thread.
190
- * The default is 120 seconds.
201
+ * Default is 120 seconds.
191
202
*/
192
203
public void setDiskExpiryThreadIntervalSeconds (int diskExpiryThreadIntervalSeconds ) {
193
204
this .diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds ;
194
205
}
195
206
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
+
196
223
/**
197
224
* Set whether to use a blocking cache that lets read attempts block
198
225
* until the requested element is created.
@@ -223,6 +250,22 @@ public void setCacheEntryFactory(CacheEntryFactory cacheEntryFactory) {
223
250
this .cacheEntryFactory = cacheEntryFactory ;
224
251
}
225
252
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
+
226
269
public void setBeanName (String name ) {
227
270
this .beanName = name ;
228
271
}
@@ -244,7 +287,7 @@ public void afterPropertiesSet() throws CacheException, IOException {
244
287
245
288
// Fetch cache region: If none with the given name exists,
246
289
// create one on the fly.
247
- Ehcache rawCache = null ;
290
+ Ehcache rawCache ;
248
291
if (this .cacheManager .cacheExists (this .cacheName )) {
249
292
if (logger .isDebugEnabled ()) {
250
293
logger .debug ("Using existing EHCache cache region '" + this .cacheName + "'" );
@@ -270,11 +313,26 @@ public void afterPropertiesSet() throws CacheException, IOException {
270
313
/**
271
314
* Create a raw Cache object based on the configuration of this FactoryBean.
272
315
*/
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 ;
278
336
}
279
337
280
338
/**
0 commit comments