001: package org.apache.turbine.services.cache;
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:
022: import java.io.Serializable;
023:
024: import org.apache.turbine.Turbine;
025:
026: /**
027: * Wrapper for an object you want to store in a cache for a period of
028: * time.
029: *
030: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
031: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
032: * @version $Id: CachedObject.java 534527 2007-05-02 16:10:59Z tv $
033: */
034: public class CachedObject implements Serializable {
035:
036: /** Serial Version UID */
037: private static final long serialVersionUID = -6196639456343534949L;
038:
039: /** Cache the object with the Default TTL */
040: public static final int DEFAULT = 0;
041:
042: /** Do not expire the object */
043: public static final int FOREVER = -1;
044:
045: /** The object to be cached. */
046: private Object contents = null;
047:
048: /** Default age (30 minutes). */
049: private long defaultage = Turbine.getConfiguration().getLong(
050: "cachedobject.defaultage", 1800000);
051:
052: /** When created. **/
053: protected long created = 0;
054:
055: /** When it expires. **/
056: private long expires = 0;
057:
058: /** Is this object stale/expired? */
059: private boolean stale = false;
060:
061: /**
062: * Constructor; sets the object to expire in the default time (30
063: * minutes).
064: *
065: * @param o The object you want to cache.
066: */
067: public CachedObject(Object o) {
068: this .contents = o;
069: this .expires = defaultage;
070: this .created = System.currentTimeMillis();
071: }
072:
073: /**
074: * Constructor.
075: *
076: * @param o The object to cache.
077: * @param expires How long before the object expires, in ms,
078: * e.g. 1000 = 1 second.
079: */
080: public CachedObject(Object o, long expires) {
081: if (expires == DEFAULT) {
082: this .expires = defaultage;
083: }
084:
085: this .contents = o;
086: this .expires = expires;
087: this .created = System.currentTimeMillis();
088: }
089:
090: /**
091: * Returns the cached object.
092: *
093: * @return The cached object.
094: */
095: public Object getContents() {
096: return contents;
097: }
098:
099: /**
100: * Returns the creation time for the object.
101: *
102: * @return When the object was created.
103: */
104: public long getCreated() {
105: return created;
106: }
107:
108: /**
109: * Returns the expiration time for the object.
110: *
111: * @return When the object expires.
112: */
113: public long getExpires() {
114: return expires;
115: }
116:
117: /**
118: * Set the expiration interval for the object.
119: *
120: * @param expires Expiration interval in millis ( 1 second = 1000 millis)
121: */
122: public void setExpires(long expires) {
123: if (expires == DEFAULT) {
124: this .expires = defaultage;
125: } else {
126: this .expires = expires;
127: }
128: if (expires == FOREVER) {
129: setStale(false);
130: } else {
131: setStale((System.currentTimeMillis() - created) > expires);
132: }
133: }
134:
135: /**
136: * Set the stale status for the object.
137: *
138: * @param stale Whether the object is stale or not.
139: */
140: public synchronized void setStale(boolean stale) {
141: this .stale = stale;
142: }
143:
144: /**
145: * Get the stale status for the object.
146: *
147: * @return Whether the object is stale or not.
148: */
149: public synchronized boolean getStale() {
150: return stale;
151: }
152:
153: /**
154: * Is the object stale?
155: *
156: * @return True if the object is stale.
157: */
158: public synchronized boolean isStale() {
159: if (expires == FOREVER) {
160: return false;
161: }
162:
163: setStale((System.currentTimeMillis() - created) > expires);
164: return getStale();
165: }
166: }
|