View Javadoc

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  /***
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,v 1.5.2.2 2004/05/20 03:05:19 seade Exp $
34   */
35  public class RefreshableCachedObject
36          extends CachedObject
37  {
38  
39      /***
40       * How long to wait before removing an untouched object from the cache.
41       * Negative numbers mean never remove (the default).
42       */
43      private long timeToLive = -1;
44  
45      /***
46       * The last time the Object was accessed from the cache.
47       */
48      private long lastAccess;
49  
50      /***
51       * Constructor; sets the object to expire in the default time (30
52       * minutes).
53       *
54       * @param o The object you want to cache.
55       */
56      public RefreshableCachedObject(Refreshable o)
57      {
58          super(o);
59          lastAccess = System.currentTimeMillis();
60      }
61  
62      /***
63       * Constructor.
64       *
65       * @param o The object to cache.
66       * @param expires How long before the object expires, in ms,
67       * e.g. 1000 = 1 second.
68       */
69      public RefreshableCachedObject(Refreshable o,
70                                     long expires)
71      {
72          super(o, expires);
73          lastAccess = System.currentTimeMillis();
74      }
75  
76      /***
77       * Sets the timeToLive value
78       *
79       * @param timeToLive the new Value in milliseconds
80       */
81      public synchronized void setTTL(long timeToLive)
82      {
83          this.timeToLive = timeToLive;
84      }
85  
86      /***
87       * Gets the timeToLive value.
88       *
89       * @return The current timeToLive value (in milliseconds)
90       */
91      public synchronized long getTTL()
92      {
93          return timeToLive;
94      }
95  
96      /***
97       * Sets the last acccess time to the current time.
98       */
99      public synchronized void touch()
100     {
101         lastAccess = System.currentTimeMillis();
102     }
103 
104     /***
105      * Returns true if the object hasn't been touched
106      * in the previous TTL period.
107      */
108     public synchronized boolean isUntouched()
109     {
110         if (timeToLive < 0)
111             return false;
112 
113         if (lastAccess + timeToLive < System.currentTimeMillis())
114             return true;
115         else
116             return false;
117     }
118 
119     /***
120      * Refresh the object and the created time.
121      */
122     public void refresh()
123     {
124         Refreshable r = (Refreshable) getContents();
125         synchronized (this)
126         {
127             created = System.currentTimeMillis();
128             r.refresh();
129         }
130     }
131 }