|
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 |
218
| public boolean isFlushed(CacheEntry cacheEntry) {
|
|
165 |
218
| if (flushDateTime != null) {
|
|
166 |
0
| long lastUpdate = cacheEntry.getLastUpdate();
|
|
167 |
| |
|
168 |
0
| return (flushDateTime.getTime() >= lastUpdate);
|
|
169 |
| } else { |
|
170 |
218
| 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 |
2003238
| 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 |
2012065
| public Object getFromCache(String key, int refreshPeriod, String cronExpiry) throws NeedsRefreshException {
|
|
237 |
2011909
| CacheEntry cacheEntry = this.getCacheEntry(key, null, null);
|
|
238 |
| |
|
239 |
2012384
| Object content = cacheEntry.getContent();
|
|
240 |
2010975
| CacheMapAccessEventType accessEventType = CacheMapAccessEventType.HIT;
|
|
241 |
| |
|
242 |
2012384
| boolean reload = false;
|
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
2012384
| if (this.isStale(cacheEntry, refreshPeriod, cronExpiry)) {
|
|
248 |
| |
|
249 |
| |
|
250 |
2012166
| EntryUpdateState updateState = getUpdateState(key);
|
|
251 |
2012166
| try {
|
|
252 |
2012166
| synchronized (updateState) {
|
|
253 |
2012166
| if (updateState.isAwaitingUpdate() || updateState.isCancelled()) {
|
|
254 |
| |
|
255 |
1979696
| updateState.startUpdate();
|
|
256 |
| |
|
257 |
1979696
| if (cacheEntry.isNew()) {
|
|
258 |
8026
| accessEventType = CacheMapAccessEventType.MISS;
|
|
259 |
| } else { |
|
260 |
1971670
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
261 |
| } |
|
262 |
32470
| } else if (updateState.isUpdating()) {
|
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
32470
| if (cacheEntry.isNew() || blocking) {
|
|
267 |
32466
| do {
|
|
268 |
126624
| try {
|
|
269 |
126624
| updateState.wait();
|
|
270 |
| } catch (InterruptedException e) { |
|
271 |
| } |
|
272 |
126624
| } while (updateState.isUpdating());
|
|
273 |
| |
|
274 |
32466
| if (updateState.isCancelled()) {
|
|
275 |
| |
|
276 |
| |
|
277 |
32446
| updateState.startUpdate();
|
|
278 |
| |
|
279 |
32446
| if (cacheEntry.isNew()) {
|
|
280 |
4
| accessEventType = CacheMapAccessEventType.MISS;
|
|
281 |
| } else { |
|
282 |
32442
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
283 |
| } |
|
284 |
20
| } else if (updateState.isComplete()) {
|
|
285 |
20
| 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 |
2012166
| releaseUpdateState(updateState, key);
|
|
298 |
| } |
|
299 |
| } |
|
300 |
| |
|
301 |
| |
|
302 |
2012384
| if (reload) {
|
|
303 |
20
| cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
304 |
| |
|
305 |
20
| if (cacheEntry != null) {
|
|
306 |
20
| 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 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
2008122
| public void cancelUpdate(String key) {
|
|
370 |
2008122
| EntryUpdateState state;
|
|
371 |
| |
|
372 |
2008122
| if (key != null) {
|
|
373 |
2008122
| synchronized (updateStates) {
|
|
374 |
2008122
| state = (EntryUpdateState) updateStates.get(key);
|
|
375 |
| |
|
376 |
2008122
| if (state != null) {
|
|
377 |
2008122
| synchronized (state) {
|
|
378 |
2008122
| int usageCounter = state.cancelUpdate();
|
|
379 |
2008122
| state.notify();
|
|
380 |
| |
|
381 |
2008122
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
382 |
| } |
|
383 |
| } else { |
|
384 |
0
| if (log.isErrorEnabled()) {
|
|
385 |
0
| log.error("internal error: expected to get a state from key [" + key + "]");
|
|
386 |
| } |
|
387 |
| } |
|
388 |
| } |
|
389 |
| } |
|
390 |
| } |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
4024308
| private void checkEntryStateUpdateUsage(String key, EntryUpdateState state, int usageCounter) {
|
|
399 |
| |
|
400 |
4024308
| if (usageCounter ==0) {
|
|
401 |
23993
| EntryUpdateState removedState = (EntryUpdateState) updateStates.remove(key);
|
|
402 |
23993
| if (state != removedState) {
|
|
403 |
0
| if (log.isErrorEnabled()) {
|
|
404 |
0
| log.error("internal error: removed state [" + removedState + "] from key [" + key + "] whereas we expected [" + state + "]");
|
|
405 |
0
| try {
|
|
406 |
0
| throw new Exception("states not equal");
|
|
407 |
| } catch (Exception e) { |
|
408 |
| |
|
409 |
0
| e.printStackTrace();
|
|
410 |
| } |
|
411 |
| } |
|
412 |
| } |
|
413 |
| } |
|
414 |
| } |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
0
| public void flushAll(Date date) {
|
|
422 |
0
| flushAll(date, null);
|
|
423 |
| } |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
0
| public void flushAll(Date date, String origin) {
|
|
432 |
0
| flushDateTime = date;
|
|
433 |
| |
|
434 |
0
| if (listenerList.getListenerCount() > 0) {
|
|
435 |
0
| dispatchCachewideEvent(CachewideEventType.CACHE_FLUSHED, date, origin);
|
|
436 |
| } |
|
437 |
| } |
|
438 |
| |
|
439 |
| |
|
440 |
| |
|
441 |
| |
|
442 |
| |
|
443 |
| |
|
444 |
| |
|
445 |
| |
|
446 |
| |
|
447 |
0
| public void flushEntry(String key) {
|
|
448 |
0
| flushEntry(key, null);
|
|
449 |
| } |
|
450 |
| |
|
451 |
| |
|
452 |
| |
|
453 |
| |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
0
| public void flushEntry(String key, String origin) {
|
|
461 |
0
| flushEntry(getCacheEntry(key, null, origin), origin);
|
|
462 |
| } |
|
463 |
| |
|
464 |
| |
|
465 |
| |
|
466 |
| |
|
467 |
| |
|
468 |
| |
|
469 |
| |
|
470 |
36
| public void flushGroup(String group) {
|
|
471 |
36
| flushGroup(group, null);
|
|
472 |
| } |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
| |
|
478 |
| |
|
479 |
| |
|
480 |
| |
|
481 |
| |
|
482 |
36
| public void flushGroup(String group, String origin) {
|
|
483 |
| |
|
484 |
36
| Set groupEntries = cacheMap.getGroup(group);
|
|
485 |
| |
|
486 |
36
| if (groupEntries != null) {
|
|
487 |
35
| Iterator itr = groupEntries.iterator();
|
|
488 |
35
| String key;
|
|
489 |
35
| CacheEntry entry;
|
|
490 |
| |
|
491 |
35
| while (itr.hasNext()) {
|
|
492 |
107
| key = (String) itr.next();
|
|
493 |
107
| entry = (CacheEntry) cacheMap.get(key);
|
|
494 |
| |
|
495 |
107
| if ((entry != null) && !entry.needsRefresh(CacheEntry.INDEFINITE_EXPIRY)) {
|
|
496 |
50
| flushEntry(entry, NESTED_EVENT);
|
|
497 |
| } |
|
498 |
| } |
|
499 |
| } |
|
500 |
| |
|
501 |
36
| if (listenerList.getListenerCount() > 0) {
|
|
502 |
36
| dispatchCacheGroupEvent(CacheEntryEventType.GROUP_FLUSHED, group, origin);
|
|
503 |
| } |
|
504 |
| } |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
| |
|
513 |
| |
|
514 |
40
| public void flushPattern(String pattern) {
|
|
515 |
40
| flushPattern(pattern, null);
|
|
516 |
| } |
|
517 |
| |
|
518 |
| |
|
519 |
| |
|
520 |
| |
|
521 |
| |
|
522 |
| |
|
523 |
| |
|
524 |
| |
|
525 |
| |
|
526 |
| |
|
527 |
40
| public void flushPattern(String pattern, String origin) {
|
|
528 |
| |
|
529 |
40
| if ((pattern != null) && (pattern.length() > 0)) {
|
|
530 |
24
| String key = null;
|
|
531 |
24
| CacheEntry entry = null;
|
|
532 |
24
| Iterator itr = cacheMap.keySet().iterator();
|
|
533 |
| |
|
534 |
24
| while (itr.hasNext()) {
|
|
535 |
72
| key = (String) itr.next();
|
|
536 |
| |
|
537 |
72
| if (key.indexOf(pattern) >= 0) {
|
|
538 |
8
| entry = (CacheEntry) cacheMap.get(key);
|
|
539 |
| |
|
540 |
8
| if (entry != null) {
|
|
541 |
8
| flushEntry(entry, origin);
|
|
542 |
| } |
|
543 |
| } |
|
544 |
| } |
|
545 |
| |
|
546 |
24
| if (listenerList.getListenerCount() > 0) {
|
|
547 |
16
| dispatchCachePatternEvent(CacheEntryEventType.PATTERN_FLUSHED, pattern, origin);
|
|
548 |
| } |
|
549 |
| } else { |
|
550 |
| |
|
551 |
| } |
|
552 |
| } |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
| |
|
558 |
| |
|
559 |
| |
|
560 |
8
| public void putInCache(String key, Object content) {
|
|
561 |
8
| putInCache(key, content, null, null, null);
|
|
562 |
| } |
|
563 |
| |
|
564 |
| |
|
565 |
| |
|
566 |
| |
|
567 |
| |
|
568 |
| |
|
569 |
| |
|
570 |
| |
|
571 |
12196
| public void putInCache(String key, Object content, EntryRefreshPolicy policy) {
|
|
572 |
12196
| putInCache(key, content, null, policy, null);
|
|
573 |
| } |
|
574 |
| |
|
575 |
| |
|
576 |
| |
|
577 |
| |
|
578 |
| |
|
579 |
| |
|
580 |
| |
|
581 |
| |
|
582 |
| |
|
583 |
84
| public void putInCache(String key, Object content, String[] groups) {
|
|
584 |
84
| putInCache(key, content, groups, null, null);
|
|
585 |
| } |
|
586 |
| |
|
587 |
| |
|
588 |
| |
|
589 |
| |
|
590 |
| |
|
591 |
| |
|
592 |
| |
|
593 |
| |
|
594 |
| |
|
595 |
| |
|
596 |
12288
| public void putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy, String origin) {
|
|
597 |
12288
| CacheEntry cacheEntry = this.getCacheEntry(key, policy, origin);
|
|
598 |
12284
| boolean isNewEntry = cacheEntry.isNew();
|
|
599 |
| |
|
600 |
| |
|
601 |
12284
| if (!isNewEntry) {
|
|
602 |
4048
| cacheEntry = new CacheEntry(key, policy);
|
|
603 |
| } |
|
604 |
| |
|
605 |
12284
| cacheEntry.setContent(content);
|
|
606 |
12284
| cacheEntry.setGroups(groups);
|
|
607 |
12284
| cacheMap.put(key, cacheEntry);
|
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
12284
| completeUpdate(key);
|
|
612 |
| |
|
613 |
12284
| if (listenerList.getListenerCount() > 0) {
|
|
614 |
120
| CacheEntryEvent event = new CacheEntryEvent(this, cacheEntry, origin);
|
|
615 |
| |
|
616 |
120
| if (isNewEntry) {
|
|
617 |
80
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_ADDED, event);
|
|
618 |
| } else { |
|
619 |
40
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_UPDATED, event);
|
|
620 |
| } |
|
621 |
| } |
|
622 |
| } |
|
623 |
| |
|
624 |
| |
|
625 |
| |
|
626 |
| |
|
627 |
| |
|
628 |
| |
|
629 |
96
| public void removeCacheEventListener(CacheEventListener listener, Class clazz) {
|
|
630 |
96
| listenerList.remove(clazz, listener);
|
|
631 |
| } |
|
632 |
| |
|
633 |
| |
|
634 |
| |
|
635 |
| |
|
636 |
| |
|
637 |
| |
|
638 |
| |
|
639 |
| |
|
640 |
| |
|
641 |
2024684
| protected CacheEntry getCacheEntry(String key, EntryRefreshPolicy policy, String origin) {
|
|
642 |
2023475
| CacheEntry cacheEntry = null;
|
|
643 |
| |
|
644 |
| |
|
645 |
2024684
| if ((key == null) || (key.length() == 0)) {
|
|
646 |
16
| throw new IllegalArgumentException("getCacheEntry called with an empty or null key");
|
|
647 |
| } |
|
648 |
| |
|
649 |
2023939
| cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
650 |
| |
|
651 |
| |
|
652 |
2024668
| if (cacheEntry == null) {
|
|
653 |
16282
| if (log.isDebugEnabled()) {
|
|
654 |
0
| log.debug("No cache entry exists for key='" + key + "', creating");
|
|
655 |
| } |
|
656 |
| |
|
657 |
16282
| cacheEntry = new CacheEntry(key, policy);
|
|
658 |
| } |
|
659 |
| |
|
660 |
2024668
| return cacheEntry;
|
|
661 |
| } |
|
662 |
| |
|
663 |
| |
|
664 |
| |
|
665 |
| |
|
666 |
| |
|
667 |
| |
|
668 |
| |
|
669 |
| |
|
670 |
| |
|
671 |
| |
|
672 |
| |
|
673 |
| |
|
674 |
2011500
| protected boolean isStale(CacheEntry cacheEntry, int refreshPeriod, String cronExpiry) {
|
|
675 |
2012384
| boolean result = cacheEntry.needsRefresh(refreshPeriod) || isFlushed(cacheEntry);
|
|
676 |
| |
|
677 |
2012384
| if ((cronExpiry != null) && (cronExpiry.length() > 0)) {
|
|
678 |
0
| try {
|
|
679 |
0
| FastCronParser parser = new FastCronParser(cronExpiry);
|
|
680 |
0
| result = result || parser.hasMoreRecentMatch(cacheEntry.getLastUpdate());
|
|
681 |
| } catch (ParseException e) { |
|
682 |
0
| log.warn(e);
|
|
683 |
| } |
|
684 |
| } |
|
685 |
| |
|
686 |
2011201
| return result;
|
|
687 |
| } |
|
688 |
| |
|
689 |
| |
|
690 |
| |
|
691 |
| |
|
692 |
| |
|
693 |
| |
|
694 |
| |
|
695 |
| |
|
696 |
| |
|
697 |
| |
|
698 |
| |
|
699 |
2011453
| protected EntryUpdateState getUpdateState(String key) {
|
|
700 |
2010724
| EntryUpdateState updateState;
|
|
701 |
| |
|
702 |
2012166
| synchronized (updateStates) {
|
|
703 |
| |
|
704 |
2012166
| updateState = (EntryUpdateState) updateStates.get(key);
|
|
705 |
| |
|
706 |
2012166
| if (updateState == null) {
|
|
707 |
| |
|
708 |
23993
| updateState = new EntryUpdateState();
|
|
709 |
23993
| updateStates.put(key, updateState);
|
|
710 |
| } else { |
|
711 |
| |
|
712 |
1988173
| updateState.incrementUsageCounter();
|
|
713 |
| } |
|
714 |
| } |
|
715 |
| |
|
716 |
2011934
| return updateState;
|
|
717 |
| } |
|
718 |
| |
|
719 |
| |
|
720 |
| |
|
721 |
| |
|
722 |
| |
|
723 |
| |
|
724 |
2012166
| protected void releaseUpdateState(EntryUpdateState state, String key) {
|
|
725 |
2012166
| synchronized (updateStates) {
|
|
726 |
2012166
| int usageCounter = state.decrementUsageCounter();
|
|
727 |
2012166
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
728 |
| } |
|
729 |
| } |
|
730 |
| |
|
731 |
| |
|
732 |
| |
|
733 |
| |
|
734 |
28
| protected void clear() {
|
|
735 |
28
| cacheMap.clear();
|
|
736 |
| } |
|
737 |
| |
|
738 |
| |
|
739 |
| |
|
740 |
| |
|
741 |
| |
|
742 |
| |
|
743 |
| |
|
744 |
| |
|
745 |
| |
|
746 |
12284
| protected void completeUpdate(String key) {
|
|
747 |
12284
| EntryUpdateState state;
|
|
748 |
| |
|
749 |
12284
| synchronized (updateStates) {
|
|
750 |
12284
| state = (EntryUpdateState) updateStates.get(key);
|
|
751 |
| |
|
752 |
12284
| if (state != null) {
|
|
753 |
4020
| synchronized (state) {
|
|
754 |
4020
| int usageCounter = state.completeUpdate();
|
|
755 |
4020
| state.notifyAll();
|
|
756 |
| |
|
757 |
4020
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
758 |
| |
|
759 |
| } |
|
760 |
| } else { |
|
761 |
| |
|
762 |
| } |
|
763 |
| } |
|
764 |
| } |
|
765 |
| |
|
766 |
| |
|
767 |
| |
|
768 |
| |
|
769 |
| |
|
770 |
| |
|
771 |
| |
|
772 |
0
| public void removeEntry(String key) {
|
|
773 |
0
| removeEntry(key, null);
|
|
774 |
| } |
|
775 |
| |
|
776 |
| |
|
777 |
| |
|
778 |
| |
|
779 |
| |
|
780 |
| |
|
781 |
| |
|
782 |
| |
|
783 |
0
| protected void removeEntry(String key, String origin) {
|
|
784 |
0
| CacheEntry cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
785 |
0
| cacheMap.remove(key);
|
|
786 |
| |
|
787 |
0
| if (listenerList.getListenerCount() > 0) {
|
|
788 |
0
| CacheEntryEvent event = new CacheEntryEvent(this, cacheEntry, origin);
|
|
789 |
0
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_REMOVED, event);
|
|
790 |
| } |
|
791 |
| } |
|
792 |
| |
|
793 |
| |
|
794 |
| |
|
795 |
| |
|
796 |
| |
|
797 |
| |
|
798 |
| |
|
799 |
174
| private void dispatchCacheEntryEvent(CacheEntryEventType eventType, CacheEntryEvent event) {
|
|
800 |
| |
|
801 |
174
| Object[] listeners = listenerList.getListenerList();
|
|
802 |
| |
|
803 |
| |
|
804 |
| |
|
805 |
174
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
806 |
348
| if (listeners[i] == CacheEntryEventListener.class) {
|
|
807 |
174
| if (eventType.equals(CacheEntryEventType.ENTRY_ADDED)) {
|
|
808 |
80
| ((CacheEntryEventListener) listeners[i + 1]).cacheEntryAdded(event);
|
|
809 |
94
| } else if (eventType.equals(CacheEntryEventType.ENTRY_UPDATED)) {
|
|
810 |
40
| ((CacheEntryEventListener) listeners[i + 1]).cacheEntryUpdated(event);
|
|
811 |
54
| } else if (eventType.equals(CacheEntryEventType.ENTRY_FLUSHED)) {
|
|
812 |
54
| ((CacheEntryEventListener) listeners[i + 1]).cacheEntryFlushed(event);
|
|
813 |
0
| } else if (eventType.equals(CacheEntryEventType.ENTRY_REMOVED)) {
|
|
814 |
0
| ((CacheEntryEventListener) listeners[i + 1]).cacheEntryRemoved(event);
|
|
815 |
| } |
|
816 |
| } |
|
817 |
| } |
|
818 |
| } |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
| |
|
824 |
| |
|
825 |
| |
|
826 |
| |
|
827 |
36
| private void dispatchCacheGroupEvent(CacheEntryEventType eventType, String group, String origin) {
|
|
828 |
36
| CacheGroupEvent event = new CacheGroupEvent(this, group, origin);
|
|
829 |
| |
|
830 |
| |
|
831 |
36
| Object[] listeners = listenerList.getListenerList();
|
|
832 |
| |
|
833 |
| |
|
834 |
| |
|
835 |
36
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
836 |
72
| if (listeners[i] == CacheEntryEventListener.class) {
|
|
837 |
36
| if (eventType.equals(CacheEntryEventType.GROUP_FLUSHED)) {
|
|
838 |
36
| ((CacheEntryEventListener) listeners[i + 1]).cacheGroupFlushed(event);
|
|
839 |
| } |
|
840 |
| } |
|
841 |
| } |
|
842 |
| } |
|
843 |
| |
|
844 |
| |
|
845 |
| |
|
846 |
| |
|
847 |
| |
|
848 |
| |
|
849 |
| |
|
850 |
| |
|
851 |
2012384
| private void dispatchCacheMapAccessEvent(CacheMapAccessEventType eventType, CacheEntry entry, String origin) {
|
|
852 |
2012384
| CacheMapAccessEvent event = new CacheMapAccessEvent(eventType, entry, origin);
|
|
853 |
| |
|
854 |
| |
|
855 |
2012384
| Object[] listeners = listenerList.getListenerList();
|
|
856 |
| |
|
857 |
| |
|
858 |
| |
|
859 |
2012384
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
860 |
368
| if (listeners[i] == CacheMapAccessEventListener.class) {
|
|
861 |
184
| ((CacheMapAccessEventListener) listeners[i + 1]).accessed(event);
|
|
862 |
| } |
|
863 |
| } |
|
864 |
| } |
|
865 |
| |
|
866 |
| |
|
867 |
| |
|
868 |
| |
|
869 |
| |
|
870 |
| |
|
871 |
| |
|
872 |
| |
|
873 |
16
| private void dispatchCachePatternEvent(CacheEntryEventType eventType, String pattern, String origin) {
|
|
874 |
16
| CachePatternEvent event = new CachePatternEvent(this, pattern, origin);
|
|
875 |
| |
|
876 |
| |
|
877 |
16
| Object[] listeners = listenerList.getListenerList();
|
|
878 |
| |
|
879 |
| |
|
880 |
| |
|
881 |
16
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
882 |
32
| if (listeners[i] == CacheEntryEventListener.class) {
|
|
883 |
16
| if (eventType.equals(CacheEntryEventType.PATTERN_FLUSHED)) {
|
|
884 |
16
| ((CacheEntryEventListener) listeners[i + 1]).cachePatternFlushed(event);
|
|
885 |
| } |
|
886 |
| } |
|
887 |
| } |
|
888 |
| } |
|
889 |
| |
|
890 |
| |
|
891 |
| |
|
892 |
| |
|
893 |
| |
|
894 |
| |
|
895 |
| |
|
896 |
0
| private void dispatchCachewideEvent(CachewideEventType eventType, Date date, String origin) {
|
|
897 |
0
| CachewideEvent event = new CachewideEvent(this, date, origin);
|
|
898 |
| |
|
899 |
| |
|
900 |
0
| Object[] listeners = listenerList.getListenerList();
|
|
901 |
| |
|
902 |
| |
|
903 |
| |
|
904 |
0
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
905 |
0
| if (listeners[i] == CacheEntryEventListener.class) {
|
|
906 |
0
| if (eventType.equals(CachewideEventType.CACHE_FLUSHED)) {
|
|
907 |
0
| ((CacheEntryEventListener) listeners[i + 1]).cacheFlushed(event);
|
|
908 |
| } |
|
909 |
| } |
|
910 |
| } |
|
911 |
| } |
|
912 |
| |
|
913 |
| |
|
914 |
| |
|
915 |
| |
|
916 |
| |
|
917 |
| |
|
918 |
| |
|
919 |
| |
|
920 |
58
| private void flushEntry(CacheEntry entry, String origin) {
|
|
921 |
58
| String key = entry.getKey();
|
|
922 |
| |
|
923 |
| |
|
924 |
58
| entry.flush();
|
|
925 |
| |
|
926 |
58
| if (!entry.isNew()) {
|
|
927 |
| |
|
928 |
58
| cacheMap.put(key, entry);
|
|
929 |
| } |
|
930 |
| |
|
931 |
| |
|
932 |
58
| if (listenerList.getListenerCount() > 0) {
|
|
933 |
54
| CacheEntryEvent event = new CacheEntryEvent(this, entry, origin);
|
|
934 |
54
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_FLUSHED, event);
|
|
935 |
| } |
|
936 |
| } |
|
937 |
| |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
32
| protected int getNbUpdateState() {
|
|
942 |
32
| synchronized(updateStates) {
|
|
943 |
32
| return updateStates.size();
|
|
944 |
| } |
|
945 |
| } |
|
946 |
| |
|
947 |
| |
|
948 |
| |
|
949 |
| |
|
950 |
| |
|
951 |
8
| public int getNbEntries() {
|
|
952 |
8
| synchronized(cacheMap) {
|
|
953 |
8
| return cacheMap.size();
|
|
954 |
| } |
|
955 |
| } |
|
956 |
| } |