1 package org.apache.jcs.engine;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.jcs.engine.behavior.ICompositeCacheAttributes;
23
24 /***
25 * The CompositeCacheAttributes defines the general cache region settings. If a region is not
26 * explicitly defined in the cache.ccf then it inherits the cache default settings.
27 * <p>
28 * If all the default attributes are not defined in the default region definition in the cache.ccf,
29 * the hard coded defaults will be used.
30 */
31 public class CompositeCacheAttributes
32 implements ICompositeCacheAttributes, Cloneable
33 {
34 private static final long serialVersionUID = 6754049978134196787L;
35
36 private static final boolean DEFAULT_USE_LATERAL = true;
37
38 private static final boolean DEFAULT_USE_REMOTE = true;
39
40 private static final boolean DEFAULT_USE_DISK = true;
41
42 private static final boolean DEFAULT_USE_SHRINKER = false;
43
44 private static final int DEFAULT_MAX_OBJECTS = 100;
45
46 private static final int DEFAULT_MAX_MEMORY_IDLE_TIME_SECONDS = 60 * 120;
47
48 private static final int DEFAULT_SHRINKER_INTERVAL_SECONDS = 30;
49
50 private static final int DEFAULT_MAX_SPOOL_PER_RUN = -1;
51
52 private static final String DEFAULT_MEMORY_CACHE_NAME = "org.apache.jcs.engine.memory.lru.LRUMemoryCache";
53
54 private boolean useLateral = DEFAULT_USE_LATERAL;
55
56 private boolean useRemote = DEFAULT_USE_REMOTE;
57
58 /*** Whether we should use a disk cache if it is configured. */
59 private boolean useDisk = DEFAULT_USE_DISK;
60
61 /*** Whether or not we should run the memory shrinker thread. */
62 private boolean useMemoryShrinker = DEFAULT_USE_SHRINKER;
63
64 /*** The maximum objects that the memory cache will be allowed to hold. */
65 private int maxObjs = DEFAULT_MAX_OBJECTS;
66
67 /*** maxMemoryIdleTimeSeconds */
68 private long maxMemoryIdleTimeSeconds = DEFAULT_MAX_MEMORY_IDLE_TIME_SECONDS;
69
70 /*** shrinkerIntervalSeconds */
71 private long shrinkerIntervalSeconds = DEFAULT_SHRINKER_INTERVAL_SECONDS;
72
73 /*** The maximum number the shrinker will spool to disk per run. */
74 private int maxSpoolPerRun = DEFAULT_MAX_SPOOL_PER_RUN;
75
76 /*** The name of this cache region. */
77 private String cacheName;
78
79 /*** The name of the memory cache implementation class. */
80 private String memoryCacheName;
81
82 private short diskUsagePattern = DISK_USAGE_PATTERN_SWAP;
83
84 /***
85 * Constructor for the CompositeCacheAttributes object
86 */
87 public CompositeCacheAttributes()
88 {
89 super();
90
91 memoryCacheName = DEFAULT_MEMORY_CACHE_NAME;
92 }
93
94 /***
95 * Sets the maxObjects attribute of the CompositeCacheAttributes object
96 * <p>
97 * @param maxObjs The new maxObjects value
98 */
99 public void setMaxObjects( int maxObjs )
100 {
101 this.maxObjs = maxObjs;
102 }
103
104 /***
105 * Gets the maxObjects attribute of the CompositeCacheAttributes object
106 * <p>
107 * @return The maxObjects value
108 */
109 public int getMaxObjects()
110 {
111 return this.maxObjs;
112 }
113
114 /***
115 * Sets the useDisk attribute of the CompositeCacheAttributes object
116 * <p>
117 * @param useDisk The new useDisk value
118 */
119 public void setUseDisk( boolean useDisk )
120 {
121 this.useDisk = useDisk;
122 }
123
124 /***
125 * Gets the useDisk attribute of the CompositeCacheAttributes object
126 * <p>
127 * @return The useDisk value
128 */
129 public boolean getUseDisk()
130 {
131 return useDisk;
132 }
133
134 /***
135 * Sets the useLateral attribute of the CompositeCacheAttributes object
136 * <p>
137 * @param b The new useLateral value
138 */
139 public void setUseLateral( boolean b )
140 {
141 this.useLateral = b;
142 }
143
144 /***
145 * Gets the useLateral attribute of the CompositeCacheAttributes object
146 * <p>
147 * @return The useLateral value
148 */
149 public boolean getUseLateral()
150 {
151 return this.useLateral;
152 }
153
154 /***
155 * Sets the useRemote attribute of the CompositeCacheAttributes object
156 * <p>
157 * @param useRemote The new useRemote value
158 */
159 public void setUseRemote( boolean useRemote )
160 {
161 this.useRemote = useRemote;
162 }
163
164 /***
165 * Gets the useRemote attribute of the CompositeCacheAttributes object
166 * <p>
167 * @return The useRemote value
168 */
169 public boolean getUseRemote()
170 {
171 return this.useRemote;
172 }
173
174 /***
175 * Sets the cacheName attribute of the CompositeCacheAttributes object
176 * <p>
177 * @param s The new cacheName value
178 */
179 public void setCacheName( String s )
180 {
181 this.cacheName = s;
182 }
183
184 /***
185 * Gets the cacheName attribute of the CompositeCacheAttributes object
186 * <p>
187 * @return The cacheName value
188 */
189 public String getCacheName()
190 {
191 return this.cacheName;
192 }
193
194 /***
195 * Sets the memoryCacheName attribute of the CompositeCacheAttributes object
196 * <p>
197 * @param s The new memoryCacheName value
198 */
199 public void setMemoryCacheName( String s )
200 {
201 this.memoryCacheName = s;
202 }
203
204 /***
205 * Gets the memoryCacheName attribute of the CompositeCacheAttributes object
206 * <p>
207 * @return The memoryCacheName value
208 */
209 public String getMemoryCacheName()
210 {
211 return this.memoryCacheName;
212 }
213
214 /***
215 * Whether the memory cache should perform background memory shrinkage.
216 * <p>
217 * @param useShrinker The new UseMemoryShrinker value
218 */
219 public void setUseMemoryShrinker( boolean useShrinker )
220 {
221 this.useMemoryShrinker = useShrinker;
222 }
223
224 /***
225 * Whether the memory cache should perform background memory shrinkage.
226 * <p>
227 * @return The UseMemoryShrinker value
228 */
229 public boolean getUseMemoryShrinker()
230 {
231 return this.useMemoryShrinker;
232 }
233
234 /***
235 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
236 * <p>
237 * @param seconds The new MaxMemoryIdleTimeSeconds value
238 */
239 public void setMaxMemoryIdleTimeSeconds( long seconds )
240 {
241 this.maxMemoryIdleTimeSeconds = seconds;
242 }
243
244 /***
245 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
246 * <p>
247 * @return The MaxMemoryIdleTimeSeconds value
248 */
249 public long getMaxMemoryIdleTimeSeconds()
250 {
251 return this.maxMemoryIdleTimeSeconds;
252 }
253
254 /***
255 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
256 * This sets the shrinker interval.
257 * <p>
258 * @param seconds The new ShrinkerIntervalSeconds value
259 */
260 public void setShrinkerIntervalSeconds( long seconds )
261 {
262 this.shrinkerIntervalSeconds = seconds;
263 }
264
265 /***
266 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
267 * This gets the shrinker interval.
268 * <p>
269 * @return The ShrinkerIntervalSeconds value
270 */
271 public long getShrinkerIntervalSeconds()
272 {
273 return this.shrinkerIntervalSeconds;
274 }
275
276 /***
277 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
278 * This sets the maximum number of items to spool per run.
279 * <p>
280 * If the value is -1, then there is no limit to the number of items to be spooled.
281 * <p>
282 * @param maxSpoolPerRun The new maxSpoolPerRun value
283 */
284 public void setMaxSpoolPerRun( int maxSpoolPerRun )
285 {
286 this.maxSpoolPerRun = maxSpoolPerRun;
287 }
288
289 /***
290 * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
291 * This gets the maximum number of items to spool per run.
292 * <p>
293 * @return The maxSpoolPerRun value
294 */
295 public int getMaxSpoolPerRun()
296 {
297 return this.maxSpoolPerRun;
298 }
299
300 /***
301 * By default this is SWAP_ONLY.
302 * <p>
303 * @param diskUsagePattern The diskUsagePattern to set.
304 */
305 public void setDiskUsagePattern( short diskUsagePattern )
306 {
307 this.diskUsagePattern = diskUsagePattern;
308 }
309
310 /***
311 * Translates the name to the disk usage pattern short value.
312 * <p>
313 * The allowed values are SWAP and UPDATE.
314 * <p>
315 * @param diskUsagePatternName The diskUsagePattern to set.
316 */
317 public void setDiskUsagePatternName( String diskUsagePatternName )
318 {
319 if ( diskUsagePatternName != null )
320 {
321 diskUsagePatternName = diskUsagePatternName.toUpperCase().trim();
322 if ( diskUsagePatternName.startsWith( "SWAP" ) )
323 {
324 this.setDiskUsagePattern( DISK_USAGE_PATTERN_SWAP );
325 }
326 else if ( diskUsagePatternName.startsWith( "UPDATE" ) )
327 {
328 this.setDiskUsagePattern( DISK_USAGE_PATTERN_UPDATE );
329 }
330 }
331 }
332
333 /***
334 * @return Returns the diskUsagePattern.
335 */
336 public short getDiskUsagePattern()
337 {
338 return diskUsagePattern;
339 }
340
341 /***
342 * Description of the Method
343 * <p>
344 * @return
345 */
346 public ICompositeCacheAttributes copy()
347 {
348 try
349 {
350 ICompositeCacheAttributes cattr = (CompositeCacheAttributes) this.clone();
351 return cattr;
352 }
353 catch ( Exception e )
354 {
355 System.err.println( e.toString() );
356 return new CompositeCacheAttributes();
357 }
358 }
359
360 /***
361 * Dumps the core attributes.
362 * <p>
363 * @return For debugging.
364 */
365 public String toString()
366 {
367 StringBuffer dump = new StringBuffer();
368
369 dump.append( "[ " );
370 dump.append( "useLateral = " ).append( useLateral );
371 dump.append( ", useRemote = " ).append( useRemote );
372 dump.append( ", useDisk = " ).append( useDisk );
373 dump.append( ", maxObjs = " ).append( maxObjs );
374 dump.append( ", maxSpoolPerRun = " ).append( maxSpoolPerRun );
375 dump.append( ", diskUsagePattern = " ).append( diskUsagePattern );
376 dump.append( " ]" );
377
378 return dump.toString();
379 }
380 }