View Javadoc

1   /**
2    *  Copyright 2003-2006 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   * />
36   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
37   * @version $Id: CacheConfiguration.java 52 2006-04-24 14:50:03Z gregluck $
38   */
39  public final class CacheConfiguration {
40  
41      /**
42       * the name of the cache.
43       */
44      protected String name;
45  
46      /**
47       * the maximum objects to be held in the {@link net.sf.ehcache.store.MemoryStore}.
48       */
49      protected int maxElementsInMemory;
50  
51      /**
52       * The policy used to evict elements from the {@link net.sf.ehcache.store.MemoryStore}.
53       * This can be one of:
54       * <ol>
55       * <li>LRU - least recently used
56       * <li>LFU - Less frequently used
57       * <li>FIFO - first in first out, the oldest element by creation time
58       * </ol>
59       * The default value is LRU
60       *
61       * @since 1.2
62       */
63      protected MemoryStoreEvictionPolicy memoryStoreEvictionPolicy;
64  
65  
66      /**
67       * Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
68       * is never expired.
69       */
70      protected boolean eternal;
71  
72      /**
73       * the time to idle for an element before it expires. Is only used
74       * if the element is not eternal.A value of 0 means do not check for idling.
75       */
76      protected int timeToIdleSeconds;
77  
78      /**
79       * Sets the time to idle for an element before it expires. Is only used
80       * if the element is not eternal. This attribute is optional in the configuration.
81       * A value of 0 means do not check time to live.
82       */
83      protected int timeToLiveSeconds;
84  
85      /**
86       * whether elements can overflow to disk when the in-memory cache
87       * has reached the set limit.
88       */
89      protected boolean overflowToDisk;
90  
91      /**
92       * For caches that overflow to disk, whether the disk cache persists between CacheManager instances.
93       */
94      protected boolean diskPersistent;
95  
96      /**
97       * The interval in seconds between runs of the disk expiry thread.
98       * <p/>
99       * 2 minutes is the default.
100      * This is not the same thing as time to live or time to idle. When the thread runs it checks
101      * these things. So this value is how often we check for expiry.
102      */
103     protected long diskExpiryThreadIntervalSeconds;
104 
105     /**
106      * The event listener factories added by BeanUtils.
107      */
108     protected final List cacheEventListenerConfigurations = new ArrayList();
109 
110     /**
111      * Sets the name of the cache. This must be unique.
112      */
113     public final void setName(String name) {
114         this.name = name;
115     }
116 
117     /**
118      * Sets the maximum objects to be held in memory.
119      */
120     public final void setMaxElementsInMemory(int maxElementsInMemory) {
121         this.maxElementsInMemory = maxElementsInMemory;
122     }
123 
124     /**
125      * Sets the eviction policy. An invalid argument will set it to null.
126      */
127     public final void setMemoryStoreEvictionPolicy(String memoryStoreEvictionPolicy) {
128         this.memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.fromString(memoryStoreEvictionPolicy);
129     }
130 
131     /**
132      * Sets whether elements are eternal. If eternal, timeouts are ignored and the element is never expired.
133      */
134     public final void setEternal(boolean eternal) {
135         this.eternal = eternal;
136     }
137 
138     /**
139      * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
140      */
141     public final void setTimeToIdleSeconds(int timeToIdleSeconds) {
142         this.timeToIdleSeconds = timeToIdleSeconds;
143     }
144 
145     /**
146      * Sets the time to idle for an element before it expires. Is only used if the element is not eternal.
147      */
148     public final void setTimeToLiveSeconds(int timeToLiveSeconds) {
149         this.timeToLiveSeconds = timeToLiveSeconds;
150     }
151 
152     /**
153      * Sets whether elements can overflow to disk when the in-memory cache has reached the set limit.
154      */
155     public final void setOverflowToDisk(boolean overflowToDisk) {
156         this.overflowToDisk = overflowToDisk;
157     }
158 
159     /**
160      * Sets whether, for caches that overflow to disk, the disk cache persist between CacheManager instances.
161      */
162     public final void setDiskPersistent(boolean diskPersistent) {
163         this.diskPersistent = diskPersistent;
164     }
165 
166     /**
167      * Sets the interval in seconds between runs of the disk expiry thread.
168      * <p/>
169      * 2 minutes is the default.
170      * This is not the same thing as time to live or time to idle. When the thread runs it checks
171      * these things. So this value is how often we check for expiry.
172      */
173     public final void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds) {
174         this.diskExpiryThreadIntervalSeconds = diskExpiryThreadIntervalSeconds;
175     }
176 
177     /**
178      * Configuration for the CachePeerListenerFactoryConfiguration.
179      */
180     public final class CacheEventListenerFactoryConfiguration extends FactoryConfiguration {
181     }
182 
183     /**
184      * Used by BeanUtils to add cacheEventListenerFactory elements to the cache configuration.
185      */
186     public final void addCacheEventListenerFactory(CacheEventListenerFactoryConfiguration factory) {
187         cacheEventListenerConfigurations.add(factory);
188     }
189 
190 }