001package org.apache.commons.jcs.engine;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
023
024/**
025 * The CompositeCacheAttributes defines the general cache region settings. If a region is not
026 * explicitly defined in the cache.ccf then it inherits the cache default settings.
027 * <p>
028 * If all the default attributes are not defined in the default region definition in the cache.ccf,
029 * the hard coded defaults will be used.
030 */
031public class CompositeCacheAttributes
032    implements ICompositeCacheAttributes
033{
034    /** Don't change */
035    private static final long serialVersionUID = 6754049978134196787L;
036
037    /** default lateral switch */
038    private static final boolean DEFAULT_USE_LATERAL = true;
039
040    /** default remote switch */
041    private static final boolean DEFAULT_USE_REMOTE = true;
042
043    /** default disk switch */
044    private static final boolean DEFAULT_USE_DISK = true;
045
046    /** default shrinker setting */
047    private static final boolean DEFAULT_USE_SHRINKER = false;
048
049    /** default max objects value */
050    private static final int DEFAULT_MAX_OBJECTS = 100;
051
052    /** default */
053    private static final int DEFAULT_MAX_MEMORY_IDLE_TIME_SECONDS = 60 * 120;
054
055    /** default interval to run the shrinker */
056    private static final int DEFAULT_SHRINKER_INTERVAL_SECONDS = 30;
057
058    /** default */
059    private static final int DEFAULT_MAX_SPOOL_PER_RUN = -1;
060
061    /** default */
062    private static final String DEFAULT_MEMORY_CACHE_NAME = "org.apache.commons.jcs.engine.memory.lru.LRUMemoryCache";
063
064    /** Default number to send to disk at a time when memory fills. */
065    private static final int DEFAULT_CHUNK_SIZE = 2;
066
067    /** allow lateral caches */
068    private boolean useLateral = DEFAULT_USE_LATERAL;
069
070    /** allow remote caches */
071    private boolean useRemote = DEFAULT_USE_REMOTE;
072
073    /** Whether we should use a disk cache if it is configured. */
074    private boolean useDisk = DEFAULT_USE_DISK;
075
076    /** Whether or not we should run the memory shrinker thread. */
077    private boolean useMemoryShrinker = DEFAULT_USE_SHRINKER;
078
079    /** The maximum objects that the memory cache will be allowed to hold. */
080    private int maxObjs = DEFAULT_MAX_OBJECTS;
081
082    /** maxMemoryIdleTimeSeconds */
083    private long maxMemoryIdleTimeSeconds = DEFAULT_MAX_MEMORY_IDLE_TIME_SECONDS;
084
085    /** shrinkerIntervalSeconds */
086    private long shrinkerIntervalSeconds = DEFAULT_SHRINKER_INTERVAL_SECONDS;
087
088    /** The maximum number the shrinker will spool to disk per run. */
089    private int maxSpoolPerRun = DEFAULT_MAX_SPOOL_PER_RUN;
090
091    /** The name of this cache region. */
092    private String cacheName;
093
094    /** The name of the memory cache implementation class. */
095    private String memoryCacheName;
096
097    /** Set via DISK_USAGE_PATTERN_NAME */
098    private DiskUsagePattern diskUsagePattern = DiskUsagePattern.SWAP;
099
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        // set this as the default so the configuration is a bit simpler
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    @Override
119    public void setMaxObjects( int maxObjs )
120    {
121        this.maxObjs = maxObjs;
122    }
123
124    /**
125     * Gets the maxObjects attribute of the CompositeCacheAttributes object
126     * <p>
127     * @return The maxObjects value
128     */
129    @Override
130    public int getMaxObjects()
131    {
132        return this.maxObjs;
133    }
134
135    /**
136     * Sets the useDisk attribute of the CompositeCacheAttributes object
137     * <p>
138     * @param useDisk The new useDisk value
139     */
140    @Override
141    public void setUseDisk( boolean useDisk )
142    {
143        this.useDisk = useDisk;
144    }
145
146    /**
147     * Gets the useDisk attribute of the CompositeCacheAttributes object
148     * <p>
149     * @return The useDisk value
150     */
151    @Override
152    public boolean isUseDisk()
153    {
154        return useDisk;
155    }
156
157    /**
158     * Sets the useLateral attribute of the CompositeCacheAttributes object
159     * <p>
160     * @param b The new useLateral value
161     */
162    @Override
163    public void setUseLateral( boolean b )
164    {
165        this.useLateral = b;
166    }
167
168    /**
169     * Gets the useLateral attribute of the CompositeCacheAttributes object
170     * <p>
171     * @return The useLateral value
172     */
173    @Override
174    public boolean isUseLateral()
175    {
176        return this.useLateral;
177    }
178
179    /**
180     * Sets the useRemote attribute of the CompositeCacheAttributes object
181     * <p>
182     * @param useRemote The new useRemote value
183     */
184    @Override
185    public void setUseRemote( boolean useRemote )
186    {
187        this.useRemote = useRemote;
188    }
189
190    /**
191     * Gets the useRemote attribute of the CompositeCacheAttributes object
192     * <p>
193     * @return The useRemote value
194     */
195    @Override
196    public boolean isUseRemote()
197    {
198        return this.useRemote;
199    }
200
201    /**
202     * Sets the cacheName attribute of the CompositeCacheAttributes object
203     * <p>
204     * @param s The new cacheName value
205     */
206    @Override
207    public void setCacheName( String s )
208    {
209        this.cacheName = s;
210    }
211
212    /**
213     * Gets the cacheName attribute of the CompositeCacheAttributes object
214     * <p>
215     * @return The cacheName value
216     */
217    @Override
218    public String getCacheName()
219    {
220        return this.cacheName;
221    }
222
223    /**
224     * Sets the memoryCacheName attribute of the CompositeCacheAttributes object
225     * <p>
226     * @param s The new memoryCacheName value
227     */
228    @Override
229    public void setMemoryCacheName( String s )
230    {
231        this.memoryCacheName = s;
232    }
233
234    /**
235     * Gets the memoryCacheName attribute of the CompositeCacheAttributes object
236     * <p>
237     * @return The memoryCacheName value
238     */
239    @Override
240    public String getMemoryCacheName()
241    {
242        return this.memoryCacheName;
243    }
244
245    /**
246     * Whether the memory cache should perform background memory shrinkage.
247     * <p>
248     * @param useShrinker The new UseMemoryShrinker value
249     */
250    @Override
251    public void setUseMemoryShrinker( boolean useShrinker )
252    {
253        this.useMemoryShrinker = useShrinker;
254    }
255
256    /**
257     * Whether the memory cache should perform background memory shrinkage.
258     * <p>
259     * @return The UseMemoryShrinker value
260     */
261    @Override
262    public boolean isUseMemoryShrinker()
263    {
264        return this.useMemoryShrinker;
265    }
266
267    /**
268     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
269     * <p>
270     * @param seconds The new MaxMemoryIdleTimeSeconds value
271     */
272    @Override
273    public void setMaxMemoryIdleTimeSeconds( long seconds )
274    {
275        this.maxMemoryIdleTimeSeconds = seconds;
276    }
277
278    /**
279     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
280     * <p>
281     * @return The MaxMemoryIdleTimeSeconds value
282     */
283    @Override
284    public long getMaxMemoryIdleTimeSeconds()
285    {
286        return this.maxMemoryIdleTimeSeconds;
287    }
288
289    /**
290     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
291     * This sets the shrinker interval.
292     * <p>
293     * @param seconds The new ShrinkerIntervalSeconds value
294     */
295    @Override
296    public void setShrinkerIntervalSeconds( long seconds )
297    {
298        this.shrinkerIntervalSeconds = seconds;
299    }
300
301    /**
302     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
303     * This gets the shrinker interval.
304     * <p>
305     * @return The ShrinkerIntervalSeconds value
306     */
307    @Override
308    public long getShrinkerIntervalSeconds()
309    {
310        return this.shrinkerIntervalSeconds;
311    }
312
313    /**
314     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
315     * This sets the maximum number of items to spool per run.
316     * <p>
317     * If the value is -1, then there is no limit to the number of items to be spooled.
318     * <p>
319     * @param maxSpoolPerRun The new maxSpoolPerRun value
320     */
321    @Override
322    public void setMaxSpoolPerRun( int maxSpoolPerRun )
323    {
324        this.maxSpoolPerRun = maxSpoolPerRun;
325    }
326
327    /**
328     * If UseMemoryShrinker is true the memory cache should auto-expire elements to reclaim space.
329     * This gets the maximum number of items to spool per run.
330     * <p>
331     * @return The maxSpoolPerRun value
332     */
333    @Override
334    public int getMaxSpoolPerRun()
335    {
336        return this.maxSpoolPerRun;
337    }
338
339    /**
340     * By default this is SWAP_ONLY.
341     * <p>
342     * @param diskUsagePattern The diskUsagePattern to set.
343     */
344    @Override
345    public void setDiskUsagePattern( DiskUsagePattern diskUsagePattern )
346    {
347        this.diskUsagePattern = diskUsagePattern;
348    }
349
350    /**
351     * Translates the name to the disk usage pattern short value.
352     * <p>
353     * The allowed values are SWAP and UPDATE.
354     * <p>
355     * @param diskUsagePatternName The diskUsagePattern to set.
356     */
357    @Override
358    public void setDiskUsagePatternName( String diskUsagePatternName )
359    {
360        if ( diskUsagePatternName != null )
361        {
362            String name = diskUsagePatternName.toUpperCase().trim();
363            if ( name.startsWith( "SWAP" ) )
364            {
365                this.setDiskUsagePattern( DiskUsagePattern.SWAP );
366            }
367            else if ( name.startsWith( "UPDATE" ) )
368            {
369                this.setDiskUsagePattern( DiskUsagePattern.UPDATE );
370            }
371        }
372    }
373
374    /**
375     * Number to send to disk at at time when memory is full.
376     * <p>
377     * @return int
378     */
379    @Override
380    public int getSpoolChunkSize()
381    {
382        return spoolChunkSize;
383    }
384
385    /**
386     * Number to send to disk at a time.
387     * <p>
388     * @param spoolChunkSize
389     */
390    @Override
391    public void setSpoolChunkSize( int spoolChunkSize )
392    {
393        this.spoolChunkSize = spoolChunkSize;
394    }
395
396    /**
397     * @return Returns the diskUsagePattern.
398     */
399    @Override
400    public DiskUsagePattern getDiskUsagePattern()
401    {
402        return diskUsagePattern;
403    }
404
405    /**
406     * Dumps the core attributes.
407     * <p>
408     * @return For debugging.
409     */
410    @Override
411    public String toString()
412    {
413        StringBuilder dump = new StringBuilder();
414
415        dump.append( "[ " );
416        dump.append( "useLateral = " ).append( useLateral );
417        dump.append( ", useRemote = " ).append( useRemote );
418        dump.append( ", useDisk = " ).append( useDisk );
419        dump.append( ", maxObjs = " ).append( maxObjs );
420        dump.append( ", maxSpoolPerRun = " ).append( maxSpoolPerRun );
421        dump.append( ", diskUsagePattern = " ).append( diskUsagePattern );
422        dump.append( ", spoolChunkSize = " ).append( spoolChunkSize );
423        dump.append( " ]" );
424
425        return dump.toString();
426    }
427
428    /**
429     * @see java.lang.Object#clone()
430     */
431    @Override
432    public ICompositeCacheAttributes clone()
433    {
434        try
435        {
436            return (ICompositeCacheAttributes)super.clone();
437        }
438        catch (CloneNotSupportedException e)
439        {
440            throw new RuntimeException("Clone not supported. This should never happen.", e);
441        }
442    }
443}