1 package org.apache.turbine.services.cache;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.Serializable;
20
21 import org.apache.turbine.Turbine;
22
23 /***
24 * Wrapper for an object you want to store in a cache for a period of
25 * time.
26 *
27 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
28 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
29 * @version $Id: CachedObject.java 278822 2005-09-05 19:53:05Z henning $
30 */
31 public class CachedObject
32 implements Serializable
33 {
34
35 /*** Serial Version UID */
36 private static final long serialVersionUID = -6196639456343534949L;
37
38 /*** Cache the object with the Default TTL */
39 public static final int DEFAULT = 0;
40
41 /*** Do not expire the object */
42 public static final int FOREVER = -1;
43
44 /*** The object to be cached. */
45 private Object contents = null;
46
47 /*** Default age (30 minutes). */
48 private long defaultage =
49 Turbine.getConfiguration()
50 .getLong("cachedobject.defaultage", 1800000);
51
52 /*** When created. **/
53 protected long created = 0;
54
55 /*** When it expires. **/
56 private long expires = 0;
57
58 /*** Is this object stale/expired? */
59 private boolean stale = false;
60
61 /***
62 * Constructor; sets the object to expire in the default time (30
63 * minutes).
64 *
65 * @param o The object you want to cache.
66 */
67 public CachedObject(Object o)
68 {
69 this.contents = o;
70 this.expires = defaultage;
71 this.created = System.currentTimeMillis();
72 }
73
74 /***
75 * Constructor.
76 *
77 * @param o The object to cache.
78 * @param expires How long before the object expires, in ms,
79 * e.g. 1000 = 1 second.
80 */
81 public CachedObject(Object o, long expires)
82 {
83 if (expires == DEFAULT)
84 {
85 this.expires = defaultage;
86 }
87
88 this.contents = o;
89 this.expires = expires;
90 this.created = System.currentTimeMillis();
91 }
92
93 /***
94 * Returns the cached object.
95 *
96 * @return The cached object.
97 */
98 public Object getContents()
99 {
100 return contents;
101 }
102
103 /***
104 * Returns the creation time for the object.
105 *
106 * @return When the object was created.
107 */
108 public long getCreated()
109 {
110 return created;
111 }
112
113 /***
114 * Returns the expiration time for the object.
115 *
116 * @return When the object expires.
117 */
118 public long getExpires()
119 {
120 return expires;
121 }
122
123 /***
124 * Set the expiration interval for the object.
125 *
126 * @param expires Expiration interval in millis ( 1 second = 1000 millis)
127 */
128 public void setExpires(long expires)
129 {
130 if (expires == DEFAULT)
131 {
132 this.expires = defaultage;
133 }
134 else
135 {
136 this.expires = expires;
137 }
138 if (expires == FOREVER)
139 {
140 setStale(false);
141 }
142 else
143 {
144 setStale((System.currentTimeMillis() - created) > expires);
145 }
146 }
147
148 /***
149 * Set the stale status for the object.
150 *
151 * @param stale Whether the object is stale or not.
152 */
153 public synchronized void setStale(boolean stale)
154 {
155 this.stale = stale;
156 }
157
158 /***
159 * Get the stale status for the object.
160 *
161 * @return Whether the object is stale or not.
162 */
163 public synchronized boolean getStale()
164 {
165 return stale;
166 }
167
168 /***
169 * Is the object stale?
170 *
171 * @return True if the object is stale.
172 */
173 public synchronized boolean isStale()
174 {
175 if (expires == FOREVER)
176 {
177 return false;
178 }
179
180 setStale((System.currentTimeMillis() - created) > expires);
181 return getStale();
182 }
183 }