001: /*--
002:
003: Copyright (C) 2000-2003 Anthony Eden.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The name "EdenLib" must not be used to endorse or promote products
019: derived from this software without prior written permission. For
020: written permission, please contact me@anthonyeden.com.
021:
022: 4. Products derived from this software may not be called "EdenLib", nor
023: may "EdenLib" appear in their name, without prior written permission
024: from Anthony Eden (me@anthonyeden.com).
025:
026: In addition, I request (but do not require) that you include in the
027: end-user documentation provided with the redistribution and/or in the
028: software itself an acknowledgement equivalent to the following:
029: "This product includes software developed by
030: Anthony Eden (http://www.anthonyeden.com/)."
031:
032: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
033: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
034: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
035: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
036: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
037: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
038: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
039: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
040: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
041: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
042: POSSIBILITY OF SUCH DAMAGE.
043:
044: For more information on EdenLib, please see <http://edenlib.sf.net/>.
045:
046: */
047:
048: package com.anthonyeden.lib.cache;
049:
050: import com.anthonyeden.lib.config.Configuration;
051: import com.anthonyeden.lib.config.ConfigurationException;
052:
053: /** Abstract implementation of the Cache interface. Cache implementations
054: can use this class as a base class for their implementation.
055:
056: @author Anthony Eden
057: @since 1.1
058: */
059:
060: public abstract class AbstractCache implements Cache {
061:
062: /** The default TTL (-1 infinate). */
063: public static final int DEFAULT_TTL = -1;
064:
065: /** The default time to live. */
066: protected int ttl = DEFAULT_TTL;
067:
068: /** Get the default time to live for the cache. A value less than 0 is
069: be considered as "infinate".
070:
071: @return The default time to live
072: */
073:
074: public int getTTL() {
075: return ttl;
076: }
077:
078: /** Set the default time to live for the cache. A value less than 0 is
079: be considered as "infinate".
080:
081: @param ttl The default time to live
082: */
083:
084: public void setTTL(int ttl) {
085: this .ttl = ttl;
086: }
087:
088: /** Set the default time to live of the Cache. This method is useful if
089: the time to live is coming from a configuration String. If the String
090: is null then the time to live is set to the value of
091: <code>AbstractCache.DEFAULT_TTL</code>.
092:
093: @param ttl The default time to live as a String
094: */
095:
096: public void setTTL(String ttl) {
097: if (ttl == null) {
098: setTTL(DEFAULT_TTL);
099: } else {
100: setTTL(Integer.parseInt(ttl));
101: }
102: }
103:
104: /** Check to see if the given CacheEntry is expired.
105:
106: @param cacheEntry The CacheEntry
107: */
108:
109: protected synchronized boolean isExpired(CacheEntry cacheEntry) {
110: // use the default TTL initially
111: int entryTTL = ttl;
112:
113: // check if the CacheEntry has specified the TTL
114: int cacheTTL = cacheEntry.getTTL();
115: if (cacheTTL >= 0) {
116: entryTTL = cacheTTL;
117: }
118:
119: // if the TTL is less then 0 then it is considered infinate
120: if (entryTTL < 0) {
121: return false;
122: }
123:
124: return System.currentTimeMillis()
125: - cacheEntry.getLastRequestTime() > entryTTL;
126: }
127:
128: /** Load the Cache's configuration from the given Configuration object.
129: This is a no-op implementation. Subclasses which require configuration
130: can override it.
131:
132: @param configuration The Configuration object
133: @throws ConfigurationException
134: */
135:
136: public void loadConfiguration(Configuration configuration)
137: throws ConfigurationException {
138: // no-op
139: }
140:
141: }
|