View Javadoc
1   package org.apache.commons.jcs3.engine;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.jcs3.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
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.commons.jcs3.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 DiskUsagePattern diskUsagePattern = DiskUsagePattern.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         // set this as the default so the configuration is a bit simpler
109         memoryCacheName = DEFAULT_MEMORY_CACHE_NAME;
110     }
111 
112     /**
113      * Sets the maxObjects attribute of the CompositeCacheAttributes object
114      * <p>
115      * @param maxObjs The new maxObjects value
116      */
117     @Override
118     public void setMaxObjects( final 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     @Override
129     public int getMaxObjects()
130     {
131         return this.maxObjs;
132     }
133 
134     /**
135      * Sets the useDisk attribute of the CompositeCacheAttributes object
136      * <p>
137      * @param useDisk The new useDisk value
138      */
139     @Override
140     public void setUseDisk( final boolean useDisk )
141     {
142         this.useDisk = useDisk;
143     }
144 
145     /**
146      * Gets the useDisk attribute of the CompositeCacheAttributes object
147      * <p>
148      * @return The useDisk value
149      */
150     @Override
151     public boolean isUseDisk()
152     {
153         return useDisk;
154     }
155 
156     /**
157      * Sets the useLateral attribute of the CompositeCacheAttributes object
158      * <p>
159      * @param b The new useLateral value
160      */
161     @Override
162     public void setUseLateral( final boolean b )
163     {
164         this.useLateral = b;
165     }
166 
167     /**
168      * Gets the useLateral attribute of the CompositeCacheAttributes object
169      * <p>
170      * @return The useLateral value
171      */
172     @Override
173     public boolean isUseLateral()
174     {
175         return this.useLateral;
176     }
177 
178     /**
179      * Sets the useRemote attribute of the CompositeCacheAttributes object
180      * <p>
181      * @param useRemote The new useRemote value
182      */
183     @Override
184     public void setUseRemote( final boolean useRemote )
185     {
186         this.useRemote = useRemote;
187     }
188 
189     /**
190      * Gets the useRemote attribute of the CompositeCacheAttributes object
191      * <p>
192      * @return The useRemote value
193      */
194     @Override
195     public boolean isUseRemote()
196     {
197         return this.useRemote;
198     }
199 
200     /**
201      * Sets the cacheName attribute of the CompositeCacheAttributes object
202      * <p>
203      * @param s The new cacheName value
204      */
205     @Override
206     public void setCacheName( final String s )
207     {
208         this.cacheName = s;
209     }
210 
211     /**
212      * Gets the cacheName attribute of the CompositeCacheAttributes object
213      * <p>
214      * @return The cacheName value
215      */
216     @Override
217     public String getCacheName()
218     {
219         return this.cacheName;
220     }
221 
222     /**
223      * Sets the memoryCacheName attribute of the CompositeCacheAttributes object
224      * <p>
225      * @param s The new memoryCacheName value
226      */
227     @Override
228     public void setMemoryCacheName( final String s )
229     {
230         this.memoryCacheName = s;
231     }
232 
233     /**
234      * Gets the memoryCacheName attribute of the CompositeCacheAttributes object
235      * <p>
236      * @return The memoryCacheName value
237      */
238     @Override
239     public String getMemoryCacheName()
240     {
241         return this.memoryCacheName;
242     }
243 
244     /**
245      * Whether the memory cache should perform background memory shrinkage.
246      * <p>
247      * @param useShrinker The new UseMemoryShrinker value
248      */
249     @Override
250     public void setUseMemoryShrinker( final boolean useShrinker )
251     {
252         this.useMemoryShrinker = useShrinker;
253     }
254 
255     /**
256      * Whether the memory cache should perform background memory shrinkage.
257      * <p>
258      * @return The UseMemoryShrinker value
259      */
260     @Override
261     public boolean isUseMemoryShrinker()
262     {
263         return this.useMemoryShrinker;
264     }
265 
266     /**
267      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
268      * <p>
269      * @param seconds The new MaxMemoryIdleTimeSeconds value
270      */
271     @Override
272     public void setMaxMemoryIdleTimeSeconds( final long seconds )
273     {
274         this.maxMemoryIdleTimeSeconds = seconds;
275     }
276 
277     /**
278      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
279      * <p>
280      * @return The MaxMemoryIdleTimeSeconds value
281      */
282     @Override
283     public long getMaxMemoryIdleTimeSeconds()
284     {
285         return this.maxMemoryIdleTimeSeconds;
286     }
287 
288     /**
289      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
290      * This sets the shrinker interval.
291      * <p>
292      * @param seconds The new ShrinkerIntervalSeconds value
293      */
294     @Override
295     public void setShrinkerIntervalSeconds( final long seconds )
296     {
297         this.shrinkerIntervalSeconds = seconds;
298     }
299 
300     /**
301      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
302      * This gets the shrinker interval.
303      * <p>
304      * @return The ShrinkerIntervalSeconds value
305      */
306     @Override
307     public long getShrinkerIntervalSeconds()
308     {
309         return this.shrinkerIntervalSeconds;
310     }
311 
312     /**
313      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
314      * This sets the maximum number of items to spool per run.
315      * <p>
316      * If the value is -1, then there is no limit to the number of items to be spooled.
317      * <p>
318      * @param maxSpoolPerRun The new maxSpoolPerRun value
319      */
320     @Override
321     public void setMaxSpoolPerRun( final int maxSpoolPerRun )
322     {
323         this.maxSpoolPerRun = maxSpoolPerRun;
324     }
325 
326     /**
327      * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
328      * This gets the maximum number of items to spool per run.
329      * <p>
330      * @return The maxSpoolPerRun value
331      */
332     @Override
333     public int getMaxSpoolPerRun()
334     {
335         return this.maxSpoolPerRun;
336     }
337 
338     /**
339      * By default this is SWAP_ONLY.
340      * <p>
341      * @param diskUsagePattern The diskUsagePattern to set.
342      */
343     @Override
344     public void setDiskUsagePattern( final DiskUsagePattern diskUsagePattern )
345     {
346         this.diskUsagePattern = diskUsagePattern;
347     }
348 
349     /**
350      * Translates the name to the disk usage pattern short value.
351      * <p>
352      * The allowed values are SWAP and UPDATE.
353      * <p>
354      * @param diskUsagePatternName The diskUsagePattern to set.
355      */
356     @Override
357     public void setDiskUsagePatternName( final String diskUsagePatternName )
358     {
359         if ( diskUsagePatternName != null )
360         {
361             final String name = diskUsagePatternName.toUpperCase().trim();
362             if ( name.startsWith( "SWAP" ) )
363             {
364                 this.setDiskUsagePattern( DiskUsagePattern.SWAP );
365             }
366             else if ( name.startsWith( "UPDATE" ) )
367             {
368                 this.setDiskUsagePattern( DiskUsagePattern.UPDATE );
369             }
370         }
371     }
372 
373     /**
374      * Number to send to disk at the time when memory is full.
375      * <p>
376      * @return int
377      */
378     @Override
379     public int getSpoolChunkSize()
380     {
381         return spoolChunkSize;
382     }
383 
384     /**
385      * Number to send to disk at a time.
386      * <p>
387      * @param spoolChunkSize
388      */
389     @Override
390     public void setSpoolChunkSize( final int spoolChunkSize )
391     {
392         this.spoolChunkSize = spoolChunkSize;
393     }
394 
395     /**
396      * @return Returns the diskUsagePattern.
397      */
398     @Override
399     public DiskUsagePattern getDiskUsagePattern()
400     {
401         return diskUsagePattern;
402     }
403 
404     /**
405      * Dumps the core attributes.
406      * <p>
407      * @return For debugging.
408      */
409     @Override
410     public String toString()
411     {
412         final StringBuilder dump = new StringBuilder();
413 
414         dump.append( "[ " );
415         dump.append( "useLateral = " ).append( useLateral );
416         dump.append( ", useRemote = " ).append( useRemote );
417         dump.append( ", useDisk = " ).append( useDisk );
418         dump.append( ", maxObjs = " ).append( maxObjs );
419         dump.append( ", maxSpoolPerRun = " ).append( maxSpoolPerRun );
420         dump.append( ", diskUsagePattern = " ).append( diskUsagePattern );
421         dump.append( ", spoolChunkSize = " ).append( spoolChunkSize );
422         dump.append( " ]" );
423 
424         return dump.toString();
425     }
426 
427     /**
428      * @see Object#clone()
429      */
430     @Override
431     public ICompositeCacheAttributes clone()
432     {
433         try
434         {
435             return (ICompositeCacheAttributes)super.clone();
436         }
437         catch (final CloneNotSupportedException e)
438         {
439             throw new RuntimeException("Clone not supported. This should never happen.", e);
440         }
441     }
442 }