1 package org.apache.fulcrum.cache;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.Serializable;
23 import java.util.concurrent.atomic.AtomicBoolean;
24
25 /**
26 * Wrapper for an object you want to store in a cache for a period of time.
27 *
28 * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
29 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
30 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
31 * @version $Id$
32 */
33
34 public class CachedObject<T> implements Serializable
35 {
36 /*
37 * TODO: The old Turbine version you could set the default age from Turbine.
38 * What we need is a CachedObjectFactory that would generate CachedObject's
39 * that could then have their default age set.
40 */
41
42 /**
43 * Serialization key
44 */
45 private static final long serialVersionUID = -9107764093769042092L;
46
47 /** Cache the object with the Default TTL */
48 public static final int DEFAULT = 0;
49
50 /** Do not expire the object */
51 public static final int FOREVER = -1;
52
53 /** The object to be cached. */
54 private T contents = null;
55
56 /** Default age (30 minutes). */
57 private static final long DEFAULT_AGE = 1_800_000;
58
59 /** When the object is created. */
60 protected long created;
61
62 /** When the object should expire. */
63 private long expires;
64
65 /** Is this object stale/expired? */
66 private final AtomicBoolean stale = new AtomicBoolean();
67
68 /**
69 * Constructor; sets the object to expire in the default time (30 minutes).
70 *
71 * @param object
72 * The object you want to cache.
73 */
74 public CachedObject(final T object)
75 {
76 this(object, DEFAULT);
77 }
78
79 /**
80 * Constructor.
81 *
82 * @param object
83 * The object to cache.
84 * @param expires
85 * How long before the object expires, in ms, e.g. 1000 = 1
86 * second.
87 */
88 public CachedObject(final T object, final long expires)
89 {
90 if (expires == DEFAULT)
91 {
92 this.expires = this.DEFAULT_AGE;
93 } else {
94 this.expires = expires;
95 }
96
97 this.contents = object;
98
99 this.created = System.currentTimeMillis();
100 }
101
102 /**
103 * Returns the cached object.
104 *
105 * @return The cached object.
106 */
107 public T getContents()
108 {
109 return this.contents;
110 }
111
112 /**
113 * Returns the creation time for the object.
114 *
115 * @return When the object was created.
116 */
117 public long getCreated()
118 {
119 return this.created;
120 }
121
122 /**
123 * Returns the expiration time for the object.
124 *
125 * @return When the object expires.
126 */
127 public long getExpires()
128 {
129 return this.expires;
130 }
131
132 /**
133 * Set the expiration interval for the object.
134 *
135 * @param expires
136 * Expiration interval in millis ( 1 second = 1000 millis)
137 */
138 public void setExpires(final long expires)
139 {
140 if (expires == DEFAULT)
141 {
142 this.expires = this.DEFAULT_AGE;
143 }
144 else
145 {
146 this.expires = expires;
147 }
148 if (expires == FOREVER)
149 {
150 setStale(false);
151 }
152 else
153 {
154 setStale((System.currentTimeMillis() - this.created) > expires);
155 }
156 }
157
158 /**
159 * Set the stale status for the object.
160 *
161 * @param stale
162 * Whether the object is stale or not.
163 */
164 public void setStale(final boolean stale)
165 {
166 this.stale.set( stale );
167 }
168
169
170 /**
171 * Is the object stale?
172 *
173 * @return True if the object is stale.
174 */
175 public boolean isStale()
176 {
177 boolean currentState = false;
178 if (this.expires != FOREVER)
179 {
180 setStale((System.currentTimeMillis() - this.created) > this.expires);
181 currentState = this.stale.get();
182 }
183 return currentState;
184 }
185 }