1 package org.apache.turbine.services.cache;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import org.apache.turbine.services.resources.TurbineResources;
58
59 /***
60 * Wrapper for an object you want to store in a cache for a period of
61 * time.
62 *
63 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
64 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
65 * @version $Id: CachedObject.java,v 1.3 2002/07/11 16:53:28 mpoeschl Exp $
66 */
67 public class CachedObject
68 implements java.io.Serializable
69 {
70
71 /*** Cache the object with the Default TTL */
72 public static final int DEFAULT = 0;
73
74 /*** Do not expire the object */
75 public static final int FOREVER = -1;
76
77 /*** The object to be cached. */
78 private Object contents = null;
79
80 /*** Default age (30 minutes). */
81 private long defaultage =
82 TurbineResources.getLong("cachedobject.defaultage", 1800000);
83
84 /*** When created. **/
85 protected long created = 0;
86
87 /*** When it expires. **/
88 private long expires = 0;
89
90 /*** Is this object stale/expired? */
91 private boolean stale = false;
92
93
94 /***
95 * Constructor; sets the object to expire in the default time (30
96 * minutes).
97 *
98 * @param o The object you want to cache.
99 */
100 public CachedObject(Object o)
101 {
102 this.contents = o;
103 this.expires = defaultage;
104 this.created = System.currentTimeMillis();
105 }
106
107 /***
108 * Constructor.
109 *
110 * @param o The object to cache.
111 * @param expires How long before the object expires, in ms,
112 * e.g. 1000 = 1 second.
113 */
114 public CachedObject(Object o, long expires)
115 {
116 if (expires == DEFAULT)
117 {
118 this.expires = defaultage;
119 }
120
121 this.contents = o;
122 this.expires = expires;
123 this.created = System.currentTimeMillis();
124 }
125
126 /***
127 * Returns the cached object.
128 *
129 * @return The cached object.
130 */
131 public Object getContents()
132 {
133 return contents;
134 }
135
136 /***
137 * Returns the creation time for the object.
138 *
139 * @return When the object was created.
140 */
141 public long getCreated()
142 {
143 return created;
144 }
145
146 /***
147 * Returns the expiration time for the object.
148 *
149 * @return When the object expires.
150 */
151 public long getExpires()
152 {
153 return expires;
154 }
155
156 /***
157 * Set the expiration interval for the object.
158 *
159 * @param expires Expiration interval in millis ( 1 second = 1000 millis)
160 */
161 public void setExpires(long expires)
162 {
163 if (expires == DEFAULT)
164 {
165 this.expires = defaultage;
166 }
167 else
168 {
169 this.expires = expires;
170 }
171 if (expires == FOREVER)
172 {
173 setStale(false);
174 }
175 else
176 {
177 setStale((System.currentTimeMillis() - created) > expires);
178 }
179 }
180
181 /***
182 * Set the stale status for the object.
183 *
184 * @param stale Whether the object is stale or not.
185 */
186 public synchronized void setStale(boolean stale)
187 {
188 this.stale = stale;
189 }
190
191 /***
192 * Get the stale status for the object.
193 *
194 * @return Whether the object is stale or not.
195 */
196 public synchronized boolean getStale()
197 {
198 return stale;
199 }
200
201 /***
202 * Is the object stale?
203 *
204 * @return True if the object is stale.
205 */
206 public synchronized boolean isStale()
207 {
208 if(expires == FOREVER)
209 {
210 return false;
211 }
212
213 setStale((System.currentTimeMillis() - created) > expires);
214 return getStale();
215 }
216 }
This page was automatically generated by Maven