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