KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > cache > CachedObject


1 package org.apache.turbine.services.cache;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.Serializable JavaDoc;
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,v 1.6.2.2 2004/05/20 03:05:19 seade Exp $
30  */

31 public class CachedObject
32         implements Serializable JavaDoc
33 {
34
35     /** Cache the object with the Default TTL */
36     public static final int DEFAULT = 0;
37
38     /** Do not expire the object */
39     public static final int FOREVER = -1;
40
41     /** The object to be cached. */
42     private Object JavaDoc contents = null;
43
44     /** Default age (30 minutes). */
45     private long defaultage =
46             Turbine.getConfiguration()
47             .getLong("cachedobject.defaultage", 1800000);
48
49     /** When created. **/
50     protected long created = 0;
51
52     /** When it expires. **/
53     private long expires = 0;
54
55     /** Is this object stale/expired? */
56     private boolean stale = false;
57
58     /**
59      * Constructor; sets the object to expire in the default time (30
60      * minutes).
61      *
62      * @param o The object you want to cache.
63      */

64     public CachedObject(Object JavaDoc o)
65     {
66         this.contents = o;
67         this.expires = defaultage;
68         this.created = System.currentTimeMillis();
69     }
70
71     /**
72      * Constructor.
73      *
74      * @param o The object to cache.
75      * @param expires How long before the object expires, in ms,
76      * e.g. 1000 = 1 second.
77      */

78     public CachedObject(Object JavaDoc o, long expires)
79     {
80         if (expires == DEFAULT)
81         {
82             this.expires = defaultage;
83         }
84
85         this.contents = o;
86         this.expires = expires;
87         this.created = System.currentTimeMillis();
88     }
89
90     /**
91      * Returns the cached object.
92      *
93      * @return The cached object.
94      */

95     public Object JavaDoc getContents()
96     {
97         return contents;
98     }
99
100     /**
101      * Returns the creation time for the object.
102      *
103      * @return When the object was created.
104      */

105     public long getCreated()
106     {
107         return created;
108     }
109
110     /**
111      * Returns the expiration time for the object.
112      *
113      * @return When the object expires.
114      */

115     public long getExpires()
116     {
117         return expires;
118     }
119
120     /**
121      * Set the expiration interval for the object.
122      *
123      * @param expires Expiration interval in millis ( 1 second = 1000 millis)
124      */

125     public void setExpires(long expires)
126     {
127         if (expires == DEFAULT)
128         {
129             this.expires = defaultage;
130         }
131         else
132         {
133             this.expires = expires;
134         }
135         if (expires == FOREVER)
136         {
137             setStale(false);
138         }
139         else
140         {
141             setStale((System.currentTimeMillis() - created) > expires);
142         }
143     }
144
145     /**
146      * Set the stale status for the object.
147      *
148      * @param stale Whether the object is stale or not.
149      */

150     public synchronized void setStale(boolean stale)
151     {
152         this.stale = stale;
153     }
154
155     /**
156      * Get the stale status for the object.
157      *
158      * @return Whether the object is stale or not.
159      */

160     public synchronized boolean getStale()
161     {
162         return stale;
163     }
164
165     /**
166      * Is the object stale?
167      *
168      * @return True if the object is stale.
169      */

170     public synchronized boolean isStale()
171     {
172         if (expires == FOREVER)
173         {
174             return false;
175         }
176
177         setStale((System.currentTimeMillis() - created) > expires);
178         return getStale();
179     }
180 }
181
Popular Tags