View Javadoc

1   package org.apache.turbine.services.cache;
2   
3   /*
4    * Copyright 2001-2005 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  /***
20   * The idea of the RefreshableCachedObject is that, rather than
21   * removing items from the cache when they become stale, we'll tell them to
22   * refresh themselves instead.  That way they'll always be in the
23   * cache, and the code to refresh them will be run by the background
24   * thread rather than by a user request thread.  You can also set a TTL (Time
25   * To Live) for the object.  This way, if the object hasn't been touched
26   * for the TTL period, then it will be removed from the cache.
27   *
28   * This extends CachedObject and provides a method for refreshing the
29   * cached object, and resetting its expire time.
30   *
31   * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
32   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
33   * @version $Id: RefreshableCachedObject.java 278822 2005-09-05 19:53:05Z henning $
34   */
35  public class RefreshableCachedObject
36          extends CachedObject
37  {
38  
39      /*** Serial Version UID */
40      private static final long serialVersionUID = 727229797378897180L;
41  
42      /***
43       * How long to wait before removing an untouched object from the cache.
44       * Negative numbers mean never remove (the default).
45       */
46      private long timeToLive = -1;
47  
48      /***
49       * The last time the Object was accessed from the cache.
50       */
51      private long lastAccess;
52  
53      /***
54       * Constructor; sets the object to expire in the default time (30
55       * minutes).
56       *
57       * @param o The object you want to cache.
58       */
59      public RefreshableCachedObject(Refreshable o)
60      {
61          super(o);
62          lastAccess = System.currentTimeMillis();
63      }
64  
65      /***
66       * Constructor.
67       *
68       * @param o The object to cache.
69       * @param expires How long before the object expires, in ms,
70       * e.g. 1000 = 1 second.
71       */
72      public RefreshableCachedObject(Refreshable o,
73                                     long expires)
74      {
75          super(o, expires);
76          lastAccess = System.currentTimeMillis();
77      }
78  
79      /***
80       * Sets the timeToLive value
81       *
82       * @param timeToLive the new Value in milliseconds
83       */
84      public synchronized void setTTL(long timeToLive)
85      {
86          this.timeToLive = timeToLive;
87      }
88  
89      /***
90       * Gets the timeToLive value.
91       *
92       * @return The current timeToLive value (in milliseconds)
93       */
94      public synchronized long getTTL()
95      {
96          return timeToLive;
97      }
98  
99      /***
100      * Sets the last acccess time to the current time.
101      */
102     public synchronized void touch()
103     {
104         lastAccess = System.currentTimeMillis();
105     }
106 
107     /***
108      * Returns true if the object hasn't been touched
109      * in the previous TTL period.
110      */
111     public synchronized boolean isUntouched()
112     {
113         if (timeToLive < 0)
114             return false;
115 
116         if (lastAccess + timeToLive < System.currentTimeMillis())
117             return true;
118         else
119             return false;
120     }
121 
122     /***
123      * Refresh the object and the created time.
124      */
125     public void refresh()
126     {
127         Refreshable r = (Refreshable) getContents();
128         synchronized (this)
129         {
130             created = System.currentTimeMillis();
131             r.refresh();
132         }
133     }
134 }