|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.base; |
|
6 |
| |
|
7 |
| import com.opensymphony.oscache.web.filter.ResponseContent; |
|
8 |
| |
|
9 |
| import java.io.Serializable; |
|
10 |
| |
|
11 |
| import java.util.Collection; |
|
12 |
| import java.util.HashSet; |
|
13 |
| import java.util.Set; |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| public class CacheEntry implements Serializable { |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| private static final byte NOT_YET = -1; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public static final int INDEFINITE_EXPIRY = -1; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| private EntryRefreshPolicy policy = null; |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| private Object content = null; |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| private Set groups = null; |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| private String key; |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| private boolean wasFlushed = false; |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| private long created = NOT_YET; |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| private long lastUpdate = NOT_YET; |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
28
| public CacheEntry(String key) {
|
|
86 |
28
| this(key, null);
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
20354
| public CacheEntry(String key, EntryRefreshPolicy policy) {
|
|
97 |
20353
| this(key, policy, null);
|
|
98 |
| } |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
20353
| public CacheEntry(String key, EntryRefreshPolicy policy, String[] groups) {
|
|
110 |
20353
| this.key = key;
|
|
111 |
| |
|
112 |
20353
| if (groups != null) {
|
|
113 |
0
| this.groups = new HashSet(groups.length);
|
|
114 |
| |
|
115 |
0
| for (int i = 0; i < groups.length; i++) {
|
|
116 |
0
| this.groups.add(groups[i]);
|
|
117 |
| } |
|
118 |
| } |
|
119 |
| |
|
120 |
20354
| this.policy = policy;
|
|
121 |
20355
| this.created = System.currentTimeMillis();
|
|
122 |
| } |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
12299
| public synchronized void setContent(Object value) {
|
|
134 |
12299
| content = value;
|
|
135 |
12300
| lastUpdate = System.currentTimeMillis();
|
|
136 |
12300
| wasFlushed = false;
|
|
137 |
| } |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
2012406
| public Object getContent() {
|
|
145 |
2012406
| return content;
|
|
146 |
| } |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
4
| public long getCreated() {
|
|
154 |
4
| return created;
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
12283
| public synchronized void setGroups(String[] groups) {
|
|
163 |
12283
| if (groups != null) {
|
|
164 |
84
| this.groups = new HashSet(groups.length);
|
|
165 |
| |
|
166 |
84
| for (int i = 0; i < groups.length; i++) {
|
|
167 |
132
| this.groups.add(groups[i]);
|
|
168 |
| } |
|
169 |
| } else { |
|
170 |
12199
| this.groups = null;
|
|
171 |
| } |
|
172 |
| |
|
173 |
12283
| lastUpdate = System.currentTimeMillis();
|
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
0
| public void setGroups(Collection groups) {
|
|
182 |
0
| if (groups != null) {
|
|
183 |
0
| this.groups = new HashSet(groups);
|
|
184 |
| } else { |
|
185 |
0
| this.groups = null;
|
|
186 |
| } |
|
187 |
| |
|
188 |
0
| lastUpdate = System.currentTimeMillis();
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
16475
| public Set getGroups() {
|
|
199 |
16475
| return groups;
|
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
310
| public String getKey() {
|
|
208 |
310
| return key;
|
|
209 |
| } |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
0
| public void setLastUpdate(long update) {
|
|
217 |
0
| lastUpdate = update;
|
|
218 |
| } |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
4
| public long getLastUpdate() {
|
|
226 |
4
| return lastUpdate;
|
|
227 |
| } |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
2061984
| public boolean isNew() {
|
|
236 |
2061984
| return lastUpdate == NOT_YET;
|
|
237 |
| } |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
0
| public int getSize() {
|
|
249 |
| |
|
250 |
0
| int size = (key.length() * 2) + 4;
|
|
251 |
| |
|
252 |
0
| if (content.getClass() == String.class) {
|
|
253 |
0
| size += ((content.toString().length() * 2) + 4);
|
|
254 |
0
| } else if (content instanceof ResponseContent) {
|
|
255 |
0
| size += ((ResponseContent) content).getSize();
|
|
256 |
| } else { |
|
257 |
0
| return -1;
|
|
258 |
| } |
|
259 |
| |
|
260 |
| |
|
261 |
0
| return size + 17;
|
|
262 |
| } |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
62
| public void flush() {
|
|
271 |
62
| wasFlushed = true;
|
|
272 |
| } |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
2012499
| public boolean needsRefresh(int refreshPeriod) {
|
|
286 |
2012500
| boolean needsRefresh;
|
|
287 |
| |
|
288 |
| |
|
289 |
2012500
| if (lastUpdate == NOT_YET) {
|
|
290 |
8042
| needsRefresh = true;
|
|
291 |
| } |
|
292 |
| |
|
293 |
2004457
| else if (wasFlushed) {
|
|
294 |
86
| needsRefresh = true;
|
|
295 |
2004372
| } else if (refreshPeriod == 0) {
|
|
296 |
2003685
| needsRefresh = true;
|
|
297 |
| } |
|
298 |
| |
|
299 |
287
| else if (policy != null) {
|
|
300 |
8
| needsRefresh = policy.needsRefresh(this);
|
|
301 |
| } |
|
302 |
| |
|
303 |
279
| else if ((refreshPeriod >= 0) && (System.currentTimeMillis() >= (lastUpdate + (refreshPeriod * 1000L)))) {
|
|
304 |
0
| needsRefresh = true;
|
|
305 |
| } else { |
|
306 |
279
| needsRefresh = false;
|
|
307 |
| } |
|
308 |
| |
|
309 |
2012502
| return needsRefresh;
|
|
310 |
| } |
|
311 |
| } |