|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| package com.opensymphony.oscache.base; |
|
6 |
| |
|
7 |
| import com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache; |
|
8 |
| import com.opensymphony.oscache.base.algorithm.LRUCache; |
|
9 |
| import com.opensymphony.oscache.base.algorithm.UnlimitedCache; |
|
10 |
| import com.opensymphony.oscache.base.events.*; |
|
11 |
| import com.opensymphony.oscache.base.persistence.PersistenceListener; |
|
12 |
| import com.opensymphony.oscache.util.FastCronParser; |
|
13 |
| |
|
14 |
| import org.apache.commons.logging.Log; |
|
15 |
| import org.apache.commons.logging.LogFactory; |
|
16 |
| |
|
17 |
| import java.io.Serializable; |
|
18 |
| |
|
19 |
| import java.text.ParseException; |
|
20 |
| |
|
21 |
| import java.util.*; |
|
22 |
| |
|
23 |
| import javax.swing.event.EventListenerList; |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| public class Cache implements Serializable { |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| public static final String NESTED_EVENT = "NESTED"; |
|
42 |
| private static transient final Log log = LogFactory.getLog(Cache.class); |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| protected EventListenerList listenerList = new EventListenerList(); |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| private AbstractConcurrentReadCache cacheMap = null; |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| private Date flushDateTime = null; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| private Map updateStates = new HashMap(); |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| private boolean blocking = false; |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
12
| public Cache(boolean useMemoryCaching, boolean unlimitedDiskCache, boolean overflowPersistence) {
|
|
93 |
12
| this(useMemoryCaching, unlimitedDiskCache, overflowPersistence, false, null, 0);
|
|
94 |
| } |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
108
| public Cache(boolean useMemoryCaching, boolean unlimitedDiskCache, boolean overflowPersistence, boolean blocking, String algorithmClass, int capacity) {
|
|
120 |
| |
|
121 |
108
| if (((algorithmClass != null) && (algorithmClass.length() > 0)) && (capacity > 0)) {
|
|
122 |
16
| try {
|
|
123 |
16
| cacheMap = (AbstractConcurrentReadCache) Class.forName(algorithmClass).newInstance();
|
|
124 |
16
| cacheMap.setMaxEntries(capacity);
|
|
125 |
| } catch (Exception e) { |
|
126 |
0
| log.error("Invalid class name for cache algorithm class. " + e.toString());
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| |
|
130 |
108
| if (cacheMap == null) {
|
|
131 |
| |
|
132 |
92
| if (capacity > 0) {
|
|
133 |
36
| cacheMap = new LRUCache(capacity);
|
|
134 |
| } else { |
|
135 |
56
| cacheMap = new UnlimitedCache();
|
|
136 |
| } |
|
137 |
| } |
|
138 |
| |
|
139 |
108
| cacheMap.setUnlimitedDiskCache(unlimitedDiskCache);
|
|
140 |
108
| cacheMap.setOverflowPersistence(overflowPersistence);
|
|
141 |
108
| cacheMap.setMemoryCaching(useMemoryCaching);
|
|
142 |
| |
|
143 |
108
| this.blocking = blocking;
|
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
16
| public void setCapacity(int capacity) {
|
|
154 |
16
| cacheMap.setMaxEntries(capacity);
|
|
155 |
| } |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
221
| public boolean isFlushed(CacheEntry cacheEntry) {
|
|
165 |
221
| if (flushDateTime != null) {
|
|
166 |
0
| long lastUpdate = cacheEntry.getLastUpdate();
|
|
167 |
| |
|
168 |
0
| return (flushDateTime.getTime() >= lastUpdate);
|
|
169 |
| } else { |
|
170 |
221
| return false;
|
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
8000
| public Object getFromCache(String key) throws NeedsRefreshException {
|
|
190 |
8000
| return getFromCache(key, CacheEntry.INDEFINITE_EXPIRY, null);
|
|
191 |
| } |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
2004396
| public Object getFromCache(String key, int refreshPeriod) throws NeedsRefreshException {
|
|
212 |
2004396
| return getFromCache(key, refreshPeriod, null);
|
|
213 |
| } |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
2012396
| public Object getFromCache(String key, int refreshPeriod, String cronExpiry) throws NeedsRefreshException {
|
|
237 |
2012396
| CacheEntry cacheEntry = this.getCacheEntry(key, null, null);
|
|
238 |
| |
|
239 |
2012381
| Object content = cacheEntry.getContent();
|
|
240 |
2012382
| CacheMapAccessEventType accessEventType = CacheMapAccessEventType.HIT;
|
|
241 |
| |
|
242 |
2012381
| boolean reload = false;
|
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
2012382
| if (this.isStale(cacheEntry, refreshPeriod, cronExpiry)) {
|
|
248 |
| |
|
249 |
| |
|
250 |
2012159
| EntryUpdateState updateState = getUpdateState(key);
|
|
251 |
2012163
| try {
|
|
252 |
2012163
| synchronized (updateState) {
|
|
253 |
2012163
| if (updateState.isAwaitingUpdate() || updateState.isCancelled()) {
|
|
254 |
| |
|
255 |
1974663
| updateState.startUpdate();
|
|
256 |
| |
|
257 |
1974663
| if (cacheEntry.isNew()) {
|
|
258 |
8026
| accessEventType = CacheMapAccessEventType.MISS;
|
|
259 |
| } else { |
|
260 |
1966637
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
261 |
| } |
|
262 |
37500
| } else if (updateState.isUpdating()) {
|
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
37500
| if (cacheEntry.isNew() || blocking) {
|
|
267 |
37496
| do {
|
|
268 |
1581251
| try {
|
|
269 |
1581251
| updateState.wait();
|
|
270 |
| } catch (InterruptedException e) { |
|
271 |
| } |
|
272 |
1581251
| } while (updateState.isUpdating());
|
|
273 |
| |
|
274 |
37496
| if (updateState.isCancelled()) {
|
|
275 |
| |
|
276 |
| |
|
277 |
37479
| updateState.startUpdate();
|
|
278 |
| |
|
279 |
37479
| if (cacheEntry.isNew()) {
|
|
280 |
4
| accessEventType = CacheMapAccessEventType.MISS;
|
|
281 |
| } else { |
|
282 |
37475
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
283 |
| } |
|
284 |
17
| } else if (updateState.isComplete()) {
|
|
285 |
17
| reload = true;
|
|
286 |
| } else { |
|
287 |
0
| log.error("Invalid update state for cache entry " + key);
|
|
288 |
| } |
|
289 |
| } |
|
290 |
| } else { |
|
291 |
0
| reload = true;
|
|
292 |
| } |
|
293 |
| } |
|
294 |
| } finally { |
|
295 |
| |
|
296 |
| |
|
297 |
2012163
| releaseUpdateState(updateState, key);
|
|
298 |
| } |
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
2012384
| if (reload) {
|
|
303 |
17
| cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
304 |
| |
|
305 |
17
| if (cacheEntry != null) {
|
|
306 |
17
| content = cacheEntry.getContent();
|
|
307 |
| } else { |
|
308 |
0
| log.error("Could not reload cache entry after waiting for it to be rebuilt");
|
|
309 |
| } |
|
310 |
| } |
|
311 |
| |
|
312 |
2012384
| dispatchCacheMapAccessEvent(accessEventType, cacheEntry, null);
|
|
313 |
| |
|
314 |
| |
|
315 |
2012384
| if (accessEventType != CacheMapAccessEventType.HIT) {
|
|
316 |
2012142
| throw new NeedsRefreshException(content);
|
|
317 |
| } |
|
318 |
| |
|
319 |
242
| return content;
|
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
54
| public void setPersistenceListener(PersistenceListener listener) {
|
|
329 |
54
| cacheMap.setPersistenceListener(listener);
|
|
330 |
| } |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
0
| public PersistenceListener getPersistenceListener() {
|
|
339 |
0
| return cacheMap.getPersistenceListener();
|
|
340 |
| } |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
96
| public void addCacheEventListener(CacheEventListener listener, Class clazz) {
|
|
349 |
96
| if (CacheEventListener.class.isAssignableFrom(clazz)) {
|
|
350 |
96
| listenerList.add(clazz, listener);
|
|
351 |
| } else { |
|
352 |
0
| log.error("The class '" + clazz.getName() + "' is not a CacheEventListener. Ignoring this listener.");
|
|
353 |
| } |
|
354 |
| } |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
0
| public EventListenerList getCacheEventListenerList() {
|
|
361 |
0
| return listenerList;
|
|
362 |
| } |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
2008121
| public void cancelUpdate(String key) {
|
|
378 |
2008121
| EntryUpdateState state;
|
|
379 |
| |
|
380 |
2008121
| if (key != null) {
|
|
381 |
2008121
| synchronized (updateStates) {
|
|
382 |
2008121
| state = (EntryUpdateState) updateStates.get(key);
|
|
383 |
| |
|
384 |
2008121
| if (state != null) {
|
|
385 |
2008121
| synchronized (state) {
|
|
386 |
2008121
| int usageCounter = state.cancelUpdate();
|
|
387 |
2008121
| state.notify();
|
|
388 |
| |
|
389 |
2008121
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
390 |
| } |
|
391 |
| } else { |
|
392 |
0
| if (log.isErrorEnabled()) {
|
|
393 |
0
| log.error("internal error: expected to get a state from key [" + key + "]");
|
|
394 |
| } |
|
395 |
| } |
|
396 |
| } |
|
397 |
| } |
|
398 |
| } |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
4024305
| private void checkEntryStateUpdateUsage(String key, EntryUpdateState state, int usageCounter) {
|
|
407 |
| |
|
408 |
4024305
| if (usageCounter ==0) {
|
|
409 |
15453
| EntryUpdateState removedState = (EntryUpdateState) updateStates.remove(key);
|
|
410 |
15453
| if (state != removedState) {
|
|
411 |
0
| if (log.isErrorEnabled()) {
|
|
412 |
0
| log.error("internal error: removed state [" + removedState + "] from key [" + key + "] whereas we expected [" + state + "]");
|
|
413 |
0
| try {
|
|
414 |
0
| throw new Exception("states not equal");
|
|
415 |
| } catch (Exception e) { |
|
416 |
| |
|
417 |
0
| e.printStackTrace();
|
|
418 |
| } |
|
419 |
| } |
|
420 |
| } |
|
421 |
| } |
|
422 |
| } |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
0
| public void flushAll(Date date) {
|
|
430 |
0
| flushAll(date, null);
|
|
431 |
| } |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |
|
437 |
| |
|
438 |
| |
|
439 |
0
| public void flushAll(Date date, String origin) {
|
|
440 |
0
| flushDateTime = date;
|
|
441 |
| |
|
442 |
0
| if (listenerList.getListenerCount() > 0) {
|
|
443 |
0
| dispatchCachewideEvent(CachewideEventType.CACHE_FLUSHED, date, origin);
|
|
444 |
| } |
|
445 |
| } |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
0
| public void flushEntry(String key) {
|
|
456 |
0
| flushEntry(key, null);
|
|
457 |
| } |
|
458 |
| |