View Javadoc

1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.config;
18  
19  import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  /**
25   * A class to represent Cache configuration.
26   * e.g.
27   * <cache name="testCache1"
28   * maxElementsInMemory="10000"
29   * eternal="false"
30   * timeToIdleSeconds="3600"
31   * timeToLiveSeconds="10"
32   * overflowToDisk="true"
33   * diskPersistent="true"
34   * diskExpiryThreadIntervalSeconds="120"
35   * maxElementsOnDisk="10000"
36   * />
37   *
38   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
39   * @version $Id: CacheConfiguration.java 512 2007-07-10 09:18:45Z gregluck $
40   */
41  public class CacheConfiguration implements Cloneable {
42  
43      /**
44       * the name of the cache.
45       */
46      protected String name;
47  
48      /**
49       * the maximum objects to be held in the {@link net.sf.ehcache.store.MemoryStore}.
50       */
51      protected int maxElementsInMemory;
52  
53      /**
54       * the maximum objects to be held in the {@link net.sf.ehcache.store.DiskStore}.
55       */
56      protected int maxElementsOnDisk;
57  
58      /**
59       * The policy used to evict elements from the {@link net.sf.ehcache.store.MemoryStore}.
60       * This can be one of:
61       * <ol>
62       * <li>LRU - least recently used
63       * <li>LFU - Less frequently used
64       * <li>FIFO - first in first out, the oldest element by creation time
65       * </ol>
66       * The default value is LRU
67       *
68       * @since 1.2
69       */
70      protected MemoryStoreEvictionPolicy memoryStoreEvictionPolicy;
71  
72  
73      /**
74       * Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
75       * is never expired.
76       */
77      protected boolean eternal;
78  
79      /**
80       * the time to idle for an element before it expires. Is only used
81       * if the element is not eternal.A value of 0 means do not check for idling.
82       */
83      protected long timeToIdleSeconds;
84  
85      /**
86       * Sets the time to idle for an element before it expires. Is only used
87       * if the element is not eternal. This attribute is optional in the configuration.
88       * A value of 0 means do not check time to live.
89       */
90      protected long timeToLiveSeconds;
91  
92      /**
93       * whether elements can overflow to disk when the in-memory cache
94       * has reached the set limit.
95       */
96      protected boolean overflowToDisk;
97  
98      /**
99       * For caches that overflow to disk, whether the disk cache persists between CacheManager instances.
100      */
101     protected boolean diskPersistent;
102 
103 
104     /**
105      * The size of the disk spool used to buffer writes
106      */
107     protected int diskSpoolBufferSizeMB;
108 
109     /**
110      * The interval in seconds between runs of the disk expiry thread.
111      * <p/>
112      * 2 minutes is the default.
113      * This is not the same thing as time to live or time to idle. When the thread runs it checks
114      * these things. So this value is how often we check for expiry.
115      */
116     protected long diskExpiryThreadIntervalSeconds;
117 
118     /**
119      * The event listener factories added by BeanUtils.
120      */
121     protected final List cacheEventListenerConfigurations = new ArrayList();
122 
123     /**
124      * The BootstrapCacheLoaderFactoryConfiguration.
125      */
126     protected BootstrapCacheLoaderFactoryConfiguration bootstrapCacheLoaderFactoryConfiguration;
127 
128 
129     /**
130      * Clones this object, following the usual contract.
131      * @return a copy, which independent other than configurations than cannot change.
132      * @throws CloneNotSupportedException
133      */
134     public Object clone() throws CloneNotSupportedException {
135         CacheConfiguration copy = (CacheConfiguration) super.clone();
136         return copy;        
137     }
138 
139     /**
140      * Sets the name of the cache. This must be unique.
141      */
142     public final void setName(String name) {
143         if (name == null) {
144             throw new IllegalArgumentException("Cache name cannot be null.");
145         }
146         if (name.indexOf('/') != -1) {
147             throw new IllegalArgumentException("Cache name cannot contain '/' characters.");
148         }
149         this.name = name;
150     }
151 
152     /**
153      * Sets the maximum objects to be held in memory.
154      */
155     public final void setMaxElementsInMemory(int maxElementsInMemory) {
156         this.maxElementsInMemory = maxElementsInMemory;
157     }
158 
159     /**
160      * Sets the eviction policy. An invalid argument will set it to null.
161      * @param memoryStoreEvictionPolicy a String representation of the policy. One of "LRU", "LFU" or "FIFO".
162      */
163     public final void setMemoryStoreEvictionPolicy(String memoryStoreEvictionPolicy) {
164         this.memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.fromString(memoryStoreEvictionPolicy);
165     }
166 
167     /**
168      * Sets the eviction policy. This method has a strange name to workaround a problem with XML parsing.
169      *
170      */
171     public final void setMemoryStoreEvictionPolicyFromObject(MemoryStoreEvictionPolicy memoryStoreEvictionPolicy) {
172         this.memoryStoreEvictionPolicy = memoryStoreEvictionPolicy;
173     }
174 
175     /**
176      * Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.
177      */
178     public final void setEternal(boolean eternal) {
179         this.eternal = eternal;
180     }
181 
182     /**
183      * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
184      */
185     public final void setTimeToIdleSeconds(long timeToIdleSeconds) {
186         this.timeToIdleSeconds = timeToIdleSeconds;
187     }
188 
189     /**
190      * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
191      */
192     public final void setTimeToLiveSeconds(long timeToLiveSeconds) {
193         this.timeToLiveSeconds = timeToLiveSeconds;
194     }
195 
196     /**
197      * Sets whether elements can overflow to disk when the in-memory cache has reached the set limit.
198      */
199     public final void setOverflowToDisk(boolean overflowToDisk) {
200         this.overflowToDisk = overflowToDisk;
201     }
202 
203     /**
204      * Sets whether, for caches that overflow to disk, the disk cache persist between CacheManager instances.
205      */
206     public final void setDiskPersistent(boolean diskPersistent) {
207         this.diskPersistent = diskPersistent;
208     }
209 
210     /**
211      * Getter
212      */
213     public int getDiskSpoolBufferSizeMB() {
214         return diskSpoolBufferSizeMB;
215     }
216 
217     /**
218      * Sets the disk spool size
219      * @param diskSpoolBufferSizeMB a postive number
220      */
221     public void setDiskSpoolBufferSizeMB(int diskSpoolBufferSizeMB) {
222         this.diskSpoolBufferSizeMB = diskSpoolBufferSizeMB;
223     }
224 
225     /**
226      * Sets the maximum number elements on Disk. 0 means unlimited.
227      */
228     public void setMaxElementsOnDisk(int maxElementsOnDisk) {
229         this.maxElementsOnDisk = maxElementsOnDisk;
230     }
231 
232     /**
233      * Sets the interval in seconds between runs of the disk expiry thread.
234      * <p/>
235      * 2 minutes is the default.
236      * This is not the same thing as time to live or time to idle. When the thread runs it checks
237      * these things. So this value is how often we check for expiry.
238      */
239     public final void setDiskExpiryThreadIntervalSeconds(long diskExpiryThreadIntervalSeconds) {
240         this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
241     }
242 
243     /**
244      * Configuration for the CachePeerListenerFactoryConfiguration.
245      */
246     public final class CacheEventListenerFactoryConfiguration extends FactoryConfiguration {
247     }
248 
249     /**
250      * Used by BeanUtils to add cacheEventListenerFactory elements to the cache configuration.
251      */
252     public final void addCacheEventListenerFactory(CacheEventListenerFactoryConfiguration factory) {
253         cacheEventListenerConfigurations.add(factory);
254     }
255 
256     /**
257      * Configuration for the BootstrapCacheLoaderFactoryConfiguration.
258      */
259     public final class BootstrapCacheLoaderFactoryConfiguration extends FactoryConfiguration {
260     }
261 
262     /**
263      * Allows {@link BeanHandler} to add the CacheManagerEventListener to the configuration.
264      */
265     public final void addBootstrapCacheLoaderFactory(BootstrapCacheLoaderFactoryConfiguration
266             bootstrapCacheLoaderFactoryConfiguration) {
267         this.bootstrapCacheLoaderFactoryConfiguration = bootstrapCacheLoaderFactoryConfiguration;
268 
269     }
270 
271     /**
272      * Accessor
273      */
274     public String getName() {
275         return name;
276     }
277 
278     /**
279      * Accessor
280      */
281     public int getMaxElementsInMemory() {
282         return maxElementsInMemory;
283     }
284 
285     /**
286      * Accessor
287      */
288     public int getMaxElementsOnDisk() {
289         return maxElementsOnDisk;
290     }
291 
292     /**
293      * Accessor
294      */
295     public MemoryStoreEvictionPolicy getMemoryStoreEvictionPolicy() {
296         return memoryStoreEvictionPolicy;
297     }
298 
299     /**
300      * Accessor
301      */
302     public boolean isEternal() {
303         return eternal;
304     }
305 
306     /**
307      * Accessor
308      */
309     public long getTimeToIdleSeconds() {
310         return timeToIdleSeconds;
311     }
312 
313     /**
314      * Accessor
315      */
316     public long getTimeToLiveSeconds() {
317         return timeToLiveSeconds;
318     }
319 
320     /**
321      * Accessor
322      */
323     public boolean isOverflowToDisk() {
324         return overflowToDisk;
325     }
326 
327     /**
328      * Accessor
329      */
330     public boolean isDiskPersistent() {
331         return diskPersistent;
332     }
333 
334     /**
335      * Accessor
336      */
337     public long getDiskExpiryThreadIntervalSeconds() {
338         return diskExpiryThreadIntervalSeconds;
339     }
340 
341     /**
342      * Accessor
343      */
344     public List getCacheEventListenerConfigurations() {
345         return cacheEventListenerConfigurations;
346     }
347 
348     /**
349      * Accessor
350      */
351     public BootstrapCacheLoaderFactoryConfiguration getBootstrapCacheLoaderFactoryConfiguration() {
352         return bootstrapCacheLoaderFactoryConfiguration;
353     }
354 
355 }