View Javadoc

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