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