%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
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; |
|
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 |
|
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 | 0 | private Object contents = null; |
43 | ||
44 | /** Default age (30 minutes). */ |
|
45 | 0 | private long defaultage = |
46 | Turbine.getConfiguration() |
|
47 | .getLong("cachedobject.defaultage", 1800000); |
|
48 | ||
49 | /** When created. **/ |
|
50 | 0 | protected long created = 0; |
51 | ||
52 | /** When it expires. **/ |
|
53 | 0 | private long expires = 0; |
54 | ||
55 | /** Is this object stale/expired? */ |
|
56 | 0 | 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 o) |
|
65 | 0 | { |
66 | 0 | this.contents = o; |
67 | 0 | this.expires = defaultage; |
68 | 0 | this.created = System.currentTimeMillis(); |
69 | 0 | } |
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 o, long expires) |
|
79 | 0 | { |
80 | 0 | if (expires == DEFAULT) |
81 | { |
|
82 | 0 | this.expires = defaultage; |
83 | } |
|
84 | ||
85 | 0 | this.contents = o; |
86 | 0 | this.expires = expires; |
87 | 0 | this.created = System.currentTimeMillis(); |
88 | 0 | } |
89 | ||
90 | /** |
|
91 | * Returns the cached object. |
|
92 | * |
|
93 | * @return The cached object. |
|
94 | */ |
|
95 | public Object getContents() |
|
96 | { |
|
97 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | if (expires == DEFAULT) |
128 | { |
|
129 | 0 | this.expires = defaultage; |
130 | } |
|
131 | else |
|
132 | { |
|
133 | 0 | this.expires = expires; |
134 | } |
|
135 | 0 | if (expires == FOREVER) |
136 | { |
|
137 | 0 | setStale(false); |
138 | } |
|
139 | else |
|
140 | { |
|
141 | 0 | setStale((System.currentTimeMillis() - created) > expires); |
142 | } |
|
143 | 0 | } |
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 | 0 | this.stale = stale; |
153 | 0 | } |
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 | 0 | 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 | 0 | if (expires == FOREVER) |
173 | { |
|
174 | 0 | return false; |
175 | } |
|
176 | ||
177 | 0 | setStale((System.currentTimeMillis() - created) > expires); |
178 | 0 | return getStale(); |
179 | } |
|
180 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |