|
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 |
40
| public Cache(boolean useMemoryCaching, boolean unlimitedDiskCache, boolean overflowPersistence) {
|
|
93 |
40
| 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 |
235
| public Cache(boolean useMemoryCaching, boolean unlimitedDiskCache, boolean overflowPersistence, boolean blocking, String algorithmClass, int capacity) {
|
|
120 |
| |
|
121 |
235
| if (((algorithmClass != null) && (algorithmClass.length() > 0)) && (capacity > 0)) {
|
|
122 |
20
| try {
|
|
123 |
20
| cacheMap = (AbstractConcurrentReadCache) Class.forName(algorithmClass).newInstance();
|
|
124 |
20
| cacheMap.setMaxEntries(capacity);
|
|
125 |
| } catch (Exception e) { |
|
126 |
0
| log.error("Invalid class name for cache algorithm class. " + e.toString());
|
|
127 |
| } |
|
128 |
| } |
|
129 |
| |
|
130 |
235
| if (cacheMap == null) {
|
|
131 |
| |
|
132 |
215
| if (capacity > 0) {
|
|
133 |
68
| cacheMap = new LRUCache(capacity);
|
|
134 |
| } else { |
|
135 |
147
| cacheMap = new UnlimitedCache();
|
|
136 |
| } |
|
137 |
| } |
|
138 |
| |
|
139 |
235
| cacheMap.setUnlimitedDiskCache(unlimitedDiskCache);
|
|
140 |
235
| cacheMap.setOverflowPersistence(overflowPersistence);
|
|
141 |
235
| cacheMap.setMemoryCaching(useMemoryCaching);
|
|
142 |
| |
|
143 |
235
| this.blocking = blocking;
|
|
144 |
| } |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
0
| public int getCapacity() {
|
|
150 |
0
| return cacheMap.getMaxEntries();
|
|
151 |
| } |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
20
| public void setCapacity(int capacity) {
|
|
161 |
20
| cacheMap.setMaxEntries(capacity);
|
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
308
| public boolean isFlushed(CacheEntry cacheEntry) {
|
|
172 |
308
| if (flushDateTime != null) {
|
|
173 |
10
| final long lastUpdate = cacheEntry.getLastUpdate();
|
|
174 |
10
| final long flushTime = flushDateTime.getTime();
|
|
175 |
| |
|
176 |
| |
|
177 |
10
| return (flushTime <= System.currentTimeMillis()) && (flushTime >= lastUpdate);
|
|
178 |
| } else { |
|
179 |
298
| return false;
|
|
180 |
| } |
|
181 |
| } |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
60010
| public Object getFromCache(String key) throws NeedsRefreshException {
|
|
199 |
60010
| return getFromCache(key, CacheEntry.INDEFINITE_EXPIRY, null);
|
|
200 |
| } |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
4505570
| public Object getFromCache(String key, int refreshPeriod) throws NeedsRefreshException {
|
|
221 |
4505570
| return getFromCache(key, refreshPeriod, null);
|
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
| |
|
241 |
| |
|
242 |
| |
|
243 |
| |
|
244 |
| |
|
245 |
4565580
| public Object getFromCache(String key, int refreshPeriod, String cronExpiry) throws NeedsRefreshException {
|
|
246 |
4565580
| CacheEntry cacheEntry = this.getCacheEntry(key, null, null);
|
|
247 |
| |
|
248 |
4564811
| Object content = cacheEntry.getContent();
|
|
249 |
4565565
| CacheMapAccessEventType accessEventType = CacheMapAccessEventType.HIT;
|
|
250 |
| |
|
251 |
4565475
| boolean reload = false;
|
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
4565565
| if (this.isStale(cacheEntry, refreshPeriod, cronExpiry)) {
|
|
257 |
| |
|
258 |
| |
|
259 |
4565262
| EntryUpdateState updateState = getUpdateState(key);
|
|
260 |
4564787
| try {
|
|
261 |
4565262
| synchronized (updateState) {
|
|
262 |
4565262
| if (updateState.isAwaitingUpdate() || updateState.isCancelled()) {
|
|
263 |
| |
|
264 |
4527583
| updateState.startUpdate();
|
|
265 |
| |
|
266 |
4527583
| if (cacheEntry.isNew()) {
|
|
267 |
60044
| accessEventType = CacheMapAccessEventType.MISS;
|
|
268 |
| } else { |
|
269 |
4467539
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
270 |
| } |
|
271 |
37679
| } else if (updateState.isUpdating()) {
|
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
37679
| if (cacheEntry.isNew() || blocking) {
|
|
276 |
37674
| do {
|
|
277 |
129432
| try {
|
|
278 |
129432
| updateState.wait();
|
|
279 |
| } catch (InterruptedException e) { |
|
280 |
| } |
|
281 |
129432
| } while (updateState.isUpdating());
|
|
282 |
| |
|
283 |
37674
| if (updateState.isCancelled()) {
|
|
284 |
| |
|
285 |
| |
|
286 |
37636
| updateState.startUpdate();
|
|
287 |
| |
|
288 |
37636
| if (cacheEntry.isNew()) {
|
|
289 |
10
| accessEventType = CacheMapAccessEventType.MISS;
|
|
290 |
| } else { |
|
291 |
37626
| accessEventType = CacheMapAccessEventType.STALE_HIT;
|
|
292 |
| } |
|
293 |
38
| } else if (updateState.isComplete()) {
|
|
294 |
38
| reload = true;
|
|
295 |
| } else { |
|
296 |
0
| log.error("Invalid update state for cache entry " + key);
|
|
297 |
| } |
|
298 |
| } |
|
299 |
| } else { |
|
300 |
0
| reload = true;
|
|
301 |
| } |
|
302 |
| } |
|
303 |
| } finally { |
|
304 |
| |
|
305 |
| |
|
306 |
4565262
| releaseUpdateState(updateState, key);
|
|
307 |
| } |
|
308 |
| } |
|
309 |
| |
|
310 |
| |
|
311 |
4565565
| if (reload) {
|
|
312 |
38
| cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
313 |
| |
|
314 |
38
| if (cacheEntry != null) {
|
|
315 |
38
| content = cacheEntry.getContent();
|
|
316 |
| } else { |
|
317 |
0
| log.error("Could not reload cache entry after waiting for it to be rebuilt");
|
|
318 |
| } |
|
319 |
| } |
|
320 |
| |
|
321 |
4565565
| dispatchCacheMapAccessEvent(accessEventType, cacheEntry, null);
|
|
322 |
| |
|
323 |
| |
|
324 |
4565565
| if (accessEventType != CacheMapAccessEventType.HIT) {
|
|
325 |
4565219
| throw new NeedsRefreshException(content);
|
|
326 |
| } |
|
327 |
| |
|
328 |
346
| return content;
|
|
329 |
| } |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
116
| public void setPersistenceListener(PersistenceListener listener) {
|
|
338 |
116
| cacheMap.setPersistenceListener(listener);
|
|
339 |
| } |
|
340 |
| |
|
341 |
| |
|
342 |
| |
|
343 |
| |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
0
| public PersistenceListener getPersistenceListener() {
|
|
348 |
0
| return cacheMap.getPersistenceListener();
|
|
349 |
| } |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
| |
|
355 |
| |
|
356 |
| |
|
357 |
| |
|
358 |
179
| public void addCacheEventListener(CacheEventListener listener) {
|
|
359 |
| |
|
360 |
179
| listenerList.add(listener.getClass(), listener);
|
|
361 |
| } |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
| |
|
368 |
| |
|
369 |
| |
|
370 |
| |
|
371 |
0
| public void addCacheEventListener(CacheEventListener listener, Class clazz) {
|
|
372 |
0
| if (CacheEventListener.class.isAssignableFrom(clazz)) {
|
|
373 |
0
| listenerList.add(clazz, listener);
|
|
374 |
| } else { |
|
375 |
0
| log.error("The class '" + clazz.getName() + "' is not a CacheEventListener. Ignoring this listener.");
|
|
376 |
| } |
|
377 |
| } |
|
378 |
| |
|
379 |
| |
|
380 |
| |
|
381 |
| |
|
382 |
| |
|
383 |
0
| public EventListenerList getCacheEventListenerList() {
|
|
384 |
0
| return listenerList;
|
|
385 |
| } |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
| |
|
394 |
| |
|
395 |
| |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
4510174
| public void cancelUpdate(String key) {
|
|
402 |
4510174
| EntryUpdateState state;
|
|
403 |
| |
|
404 |
4510174
| if (key != null) {
|
|
405 |
4510174
| synchronized (updateStates) {
|
|
406 |
4510174
| state = (EntryUpdateState) updateStates.get(key);
|
|
407 |
| |
|
408 |
4510174
| if (state != null) {
|
|
409 |
4510174
| synchronized (state) {
|
|
410 |
4510174
| int usageCounter = state.cancelUpdate();
|
|
411 |
4510174
| state.notify();
|
|
412 |
| |
|
413 |
4510174
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
414 |
| } |
|
415 |
| } else { |
|
416 |
0
| if (log.isErrorEnabled()) {
|
|
417 |
0
| log.error("internal error: expected to get a state from key [" + key + "]");
|
|
418 |
| } |
|
419 |
| } |
|
420 |
| } |
|
421 |
| } |
|
422 |
| } |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
9130481
| private void checkEntryStateUpdateUsage(String key, EntryUpdateState state, int usageCounter) {
|
|
431 |
| |
|
432 |
9130481
| if (usageCounter == 0) {
|
|
433 |
205224
| EntryUpdateState removedState = (EntryUpdateState) updateStates.remove(key);
|
|
434 |
205224
| if (state != removedState) {
|
|
435 |
0
| if (log.isErrorEnabled()) {
|
|
436 |
0
| try {
|
|
437 |
0
| throw new Exception("OSCache: internal error: removed state [" + removedState + "] from key [" + key + "] whereas we expected [" + state + "]");
|
|
438 |
| } catch (Exception e) { |
|
439 |
0
| log.error(e);
|
|
440 |
| } |
|
441 |
| } |
|
442 |
| } |
|
443 |
| } |
|
444 |
| } |
|
445 |
| |
|
446 |
| |
|
447 |
| |
|
448 |
| |
|
449 |
| |
|
450 |
| |
|
451 |
10
| public void flushAll(Date date) {
|
|
452 |
10
| flushAll(date, null);
|
|
453 |
| } |
|
454 |
| |
|
455 |
| |
|
456 |
| |
|
457 |
| |
|
458 |
| |
|
459 |
| |
|
460 |
| |
|
461 |
10
| public void flushAll(Date date, String origin) {
|
|
462 |
10
| flushDateTime = date;
|
|
463 |
| |
|
464 |
10
| if (listenerList.getListenerCount() > 0) {
|
|
465 |
6
| dispatchCachewideEvent(CachewideEventType.CACHE_FLUSHED, date, origin);
|
|
466 |
| } |
|
467 |
| } |
|
468 |
| |
|
469 |
| |
|
470 |
| |
|
471 |
| |
|
472 |
| |
|
473 |
| |
|
474 |
| |
|
475 |
| |
|
476 |
| |
|
477 |
10
| public void flushEntry(String key) {
|
|
478 |
10
| flushEntry(key, null);
|
|
479 |
| } |
|
480 |
| |
|
481 |
| |
|
482 |
| |
|
483 |
| |
|
484 |
| |
|
485 |
| |
|
486 |
| |
|
487 |
| |
|
488 |
| |
|
489 |
| |
|
490 |
10
| public void flushEntry(String key, String origin) {
|
|
491 |
10
| flushEntry(getCacheEntry(key, null, origin), origin);
|
|
492 |
| } |
|
493 |
| |
|
494 |
| |
|
495 |
| |
|
496 |
| |
|
497 |
| |
|
498 |
| |
|
499 |
| |
|
500 |
45
| public void flushGroup(String group) {
|
|
501 |
45
| flushGroup(group, null);
|
|
502 |
| } |
|
503 |
| |
|
504 |
| |
|
505 |
| |
|
506 |
| |
|
507 |
| |
|
508 |
| |
|
509 |
| |
|
510 |
| |
|
511 |
| |
|
512 |
45
| public void flushGroup(String group, String origin) {
|
|
513 |
| |
|
514 |
45
| Set groupEntries = cacheMap.getGroup(group);
|
|
515 |
| |
|
516 |
45
| if (groupEntries != null) {
|
|
517 |
44
| Iterator itr = groupEntries.iterator();
|
|
518 |
44
| String key;
|
|
519 |
44
| CacheEntry entry;
|
|
520 |
| |
|
521 |
44
| while (itr.hasNext()) {
|
|
522 |
139
| key = (String) itr.next();
|
|
523 |
139
| entry = (CacheEntry) cacheMap.get(key);
|
|
524 |
| |
|
525 |
139
| if ((entry != null) && !entry.needsRefresh(CacheEntry.INDEFINITE_EXPIRY)) {
|
|
526 |
64
| flushEntry(entry, NESTED_EVENT);
|
|
527 |
| } |
|
528 |
| } |
|
529 |
| } |
|
530 |
| |
|
531 |
45
| if (listenerList.getListenerCount() > 0) {
|
|
532 |
45
| dispatchCacheGroupEvent(CacheEntryEventType.GROUP_FLUSHED, group, origin);
|
|
533 |
| } |
|
534 |
| } |
|
535 |
| |
|
536 |
| |
|
537 |
| |
|
538 |
| |
|
539 |
| |
|
540 |
| |
|
541 |
| |
|
542 |
| |
|
543 |
| |
|
544 |
50
| public void flushPattern(String pattern) {
|
|
545 |
50
| flushPattern(pattern, null);
|
|
546 |
| } |
|
547 |
| |
|
548 |
| |
|
549 |
| |
|
550 |
| |
|
551 |
| |
|
552 |
| |
|
553 |
| |
|
554 |
| |
|
555 |
| |
|
556 |
| |
|
557 |
50
| public void flushPattern(String pattern, String origin) {
|
|
558 |
| |
|
559 |
50
| if ((pattern != null) && (pattern.length() > 0)) {
|
|
560 |
30
| String key = null;
|
|
561 |
30
| CacheEntry entry = null;
|
|
562 |
30
| Iterator itr = cacheMap.keySet().iterator();
|
|
563 |
| |
|
564 |
30
| while (itr.hasNext()) {
|
|
565 |
90
| key = (String) itr.next();
|
|
566 |
| |
|
567 |
90
| if (key.indexOf(pattern) >= 0) {
|
|
568 |
10
| entry = (CacheEntry) cacheMap.get(key);
|
|
569 |
| |
|
570 |
10
| if (entry != null) {
|
|
571 |
10
| flushEntry(entry, origin);
|
|
572 |
| } |
|
573 |
| } |
|
574 |
| } |
|
575 |
| |
|
576 |
30
| if (listenerList.getListenerCount() > 0) {
|
|
577 |
22
| dispatchCachePatternEvent(CacheEntryEventType.PATTERN_FLUSHED, pattern, origin);
|
|
578 |
| } |
|
579 |
| } else { |
|
580 |
| |
|
581 |
| } |
|
582 |
| } |
|
583 |
| |
|
584 |
| |
|
585 |
| |
|
586 |
| |
|
587 |
| |
|
588 |
| |
|
589 |
| |
|
590 |
10
| public void putInCache(String key, Object content) {
|
|
591 |
10
| putInCache(key, content, null, null, null);
|
|
592 |
| } |
|
593 |
| |
|
594 |
| |
|
595 |
| |
|
596 |
| |
|
597 |
| |
|
598 |
| |
|
599 |
| |
|
600 |
| |
|
601 |
65295
| public void putInCache(String key, Object content, EntryRefreshPolicy policy) {
|
|
602 |
65295
| putInCache(key, content, null, policy, null);
|
|
603 |
| } |
|
604 |
| |
|
605 |
| |
|
606 |
| |
|
607 |
| |
|
608 |
| |
|
609 |
| |
|
610 |
| |
|
611 |
| |
|
612 |
| |
|
613 |
105
| public void putInCache(String key, Object content, String[] groups) {
|
|
614 |
105
| putInCache(key, content, groups, null, null);
|
|
615 |
| } |
|
616 |
| |
|
617 |
| |
|
618 |
| |
|
619 |
| |
|
620 |
| |
|
621 |
| |
|
622 |
| |
|
623 |
| |
|
624 |
| |
|
625 |
| |
|
626 |
65410
| public void putInCache(String key, Object content, String[] groups, EntryRefreshPolicy policy, String origin) {
|
|
627 |
65410
| CacheEntry cacheEntry = this.getCacheEntry(key, policy, origin);
|
|
628 |
65405
| boolean isNewEntry = cacheEntry.isNew();
|
|
629 |
| |
|
630 |
| |
|
631 |
65405
| if (!isNewEntry) {
|
|
632 |
5080
| cacheEntry = new CacheEntry(key, policy);
|
|
633 |
| } |
|
634 |
| |
|
635 |
65405
| cacheEntry.setContent(content);
|
|
636 |
65405
| cacheEntry.setGroups(groups);
|
|
637 |
65405
| cacheMap.put(key, cacheEntry);
|
|
638 |
| |
|
639 |
| |
|
640 |
| |
|
641 |
65405
| completeUpdate(key);
|
|
642 |
| |
|
643 |
65405
| if (listenerList.getListenerCount() > 0) {
|
|
644 |
199
| CacheEntryEvent event = new CacheEntryEvent(this, cacheEntry, origin);
|
|
645 |
| |
|
646 |
199
| if (isNewEntry) {
|
|
647 |
138
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_ADDED, event);
|
|
648 |
| } else { |
|
649 |
61
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_UPDATED, event);
|
|
650 |
| } |
|
651 |
| } |
|
652 |
| } |
|
653 |
| |
|
654 |
| |
|
655 |
| |
|
656 |
| |
|
657 |
| |
|
658 |
| |
|
659 |
| |
|
660 |
| |
|
661 |
0
| public void removeCacheEventListener(CacheEventListener listener, Class clazz) {
|
|
662 |
0
| listenerList.remove(clazz, listener);
|
|
663 |
| } |
|
664 |
| |
|
665 |
| |
|
666 |
| |
|
667 |
| |
|
668 |
| |
|
669 |
| |
|
670 |
| |
|
671 |
150
| public void removeCacheEventListener(CacheEventListener listener) {
|
|
672 |
| |
|
673 |
150
| listenerList.remove(listener.getClass(), listener);
|
|
674 |
| } |
|
675 |
| |
|
676 |
| |
|
677 |
| |
|
678 |
| |
|
679 |
| |
|
680 |
| |
|
681 |
| |
|
682 |
| |
|
683 |
| |
|
684 |
4631000
| protected CacheEntry getCacheEntry(String key, EntryRefreshPolicy policy, String origin) {
|
|
685 |
4630940
| CacheEntry cacheEntry = null;
|
|
686 |
| |
|
687 |
| |
|
688 |
4631000
| if ((key == null) || (key.length() == 0)) {
|
|
689 |
20
| throw new IllegalArgumentException("getCacheEntry called with an empty or null key");
|
|
690 |
| } |
|
691 |
| |
|
692 |
4630755
| cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
693 |
| |
|
694 |
| |
|
695 |
4630980
| if (cacheEntry == null) {
|
|
696 |
120417
| if (log.isDebugEnabled()) {
|
|
697 |
0
| log.debug("No cache entry exists for key='" + key + "', creating");
|
|
698 |
| } |
|
699 |
| |
|
700 |
120417
| cacheEntry = new CacheEntry(key, policy);
|
|
701 |
| } |
|
702 |
| |
|
703 |
4630798
| return cacheEntry;
|
|
704 |
| } |
|
705 |
| |
|
706 |
| |
|
707 |
| |
|
708 |
| |
|
709 |
| |
|
710 |
| |
|
711 |
| |
|
712 |
| |
|
713 |
| |
|
714 |
| |
|
715 |
| |
|
716 |
| |
|
717 |
4565565
| protected boolean isStale(CacheEntry cacheEntry, int refreshPeriod, String cronExpiry) {
|
|
718 |
4564560
| boolean result = cacheEntry.needsRefresh(refreshPeriod) || isFlushed(cacheEntry);
|
|
719 |
| |
|
720 |
4564932
| if ((!result) && (cronExpiry != null) && (cronExpiry.length() > 0)) {
|
|
721 |
0
| try {
|
|
722 |
0
| FastCronParser parser = new FastCronParser(cronExpiry);
|
|
723 |
0
| result = result || parser.hasMoreRecentMatch(cacheEntry.getLastUpdate());
|
|
724 |
| } catch (ParseException e) { |
|
725 |
0
| log.warn(e);
|
|
726 |
| } |
|
727 |
| } |
|
728 |
| |
|
729 |
4565565
| return result;
|
|
730 |
| } |
|
731 |
| |
|
732 |
| |
|
733 |
| |
|
734 |
| |
|
735 |
| |
|
736 |
| |
|
737 |
| |
|
738 |
| |
|
739 |
| |
|
740 |
| |
|
741 |
| |
|
742 |
4565262
| protected EntryUpdateState getUpdateState(String key) {
|
|
743 |
4565262
| EntryUpdateState updateState;
|
|
744 |
| |
|
745 |
4565262
| synchronized (updateStates) {
|
|
746 |
| |
|
747 |
4565262
| updateState = (EntryUpdateState) updateStates.get(key);
|
|
748 |
| |
|
749 |
4565262
| if (updateState == null) {
|
|
750 |
| |
|
751 |
205224
| updateState = new EntryUpdateState();
|
|
752 |
205224
| updateStates.put(key, updateState);
|
|
753 |
| } else { |
|
754 |
| |
|
755 |
4360038
| updateState.incrementUsageCounter();
|
|
756 |
| } |
|
757 |
| } |
|
758 |
| |
|
759 |
4565262
| return updateState;
|
|
760 |
| } |
|
761 |
| |
|
762 |
| |
|
763 |
| |
|
764 |
| |
|
765 |
| |
|
766 |
| |
|
767 |
4565262
| protected void releaseUpdateState(EntryUpdateState state, String key) {
|
|
768 |
4565262
| synchronized (updateStates) {
|
|
769 |
4565262
| int usageCounter = state.decrementUsageCounter();
|
|
770 |
4565262
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
771 |
| } |
|
772 |
| } |
|
773 |
| |
|
774 |
| |
|
775 |
| |
|
776 |
| |
|
777 |
35
| protected void clear() {
|
|
778 |
35
| cacheMap.clear();
|
|
779 |
| } |
|
780 |
| |
|
781 |
| |
|
782 |
| |
|
783 |
| |
|
784 |
| |
|
785 |
| |
|
786 |
| |
|
787 |
| |
|
788 |
| |
|
789 |
65405
| protected void completeUpdate(String key) {
|
|
790 |
65405
| EntryUpdateState state;
|
|
791 |
| |
|
792 |
65405
| synchronized (updateStates) {
|
|
793 |
65405
| state = (EntryUpdateState) updateStates.get(key);
|
|
794 |
| |
|
795 |
65405
| if (state != null) {
|
|
796 |
55045
| synchronized (state) {
|
|
797 |
55045
| int usageCounter = state.completeUpdate();
|
|
798 |
55045
| state.notifyAll();
|
|
799 |
| |
|
800 |
55045
| checkEntryStateUpdateUsage(key, state, usageCounter);
|
|
801 |
| |
|
802 |
| } |
|
803 |
| } else { |
|
804 |
| |
|
805 |
| } |
|
806 |
| } |
|
807 |
| } |
|
808 |
| |
|
809 |
| |
|
810 |
| |
|
811 |
| |
|
812 |
| |
|
813 |
| |
|
814 |
| |
|
815 |
0
| public void removeEntry(String key) {
|
|
816 |
0
| removeEntry(key, null);
|
|
817 |
| } |
|
818 |
| |
|
819 |
| |
|
820 |
| |
|
821 |
| |
|
822 |
| |
|
823 |
| |
|
824 |
| |
|
825 |
| |
|
826 |
0
| protected void removeEntry(String key, String origin) {
|
|
827 |
0
| CacheEntry cacheEntry = (CacheEntry) cacheMap.get(key);
|
|
828 |
0
| cacheMap.remove(key);
|
|
829 |
| |
|
830 |
0
| if (listenerList.getListenerCount() > 0) {
|
|
831 |
0
| CacheEntryEvent event = new CacheEntryEvent(this, cacheEntry, origin);
|
|
832 |
0
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_REMOVED, event);
|
|
833 |
| } |
|
834 |
| } |
|
835 |
| |
|
836 |
| |
|
837 |
| |
|
838 |
| |
|
839 |
| |
|
840 |
| |
|
841 |
| |
|
842 |
279
| private void dispatchCacheEntryEvent(CacheEntryEventType eventType, CacheEntryEvent event) {
|
|
843 |
| |
|
844 |
279
| Object[] listeners = listenerList.getListenerList();
|
|
845 |
| |
|
846 |
| |
|
847 |
| |
|
848 |
279
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
849 |
565
| if (listeners[i+1] instanceof CacheEntryEventListener) {
|
|
850 |
326
| CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
|
|
851 |
326
| if (eventType.equals(CacheEntryEventType.ENTRY_ADDED)) {
|
|
852 |
164
| listener.cacheEntryAdded(event);
|
|
853 |
162
| } else if (eventType.equals(CacheEntryEventType.ENTRY_UPDATED)) {
|
|
854 |
67
| listener.cacheEntryUpdated(event);
|
|
855 |
95
| } else if (eventType.equals(CacheEntryEventType.ENTRY_FLUSHED)) {
|
|
856 |
95
| listener.cacheEntryFlushed(event);
|
|
857 |
0
| } else if (eventType.equals(CacheEntryEventType.ENTRY_REMOVED)) {
|
|
858 |
0
| listener.cacheEntryRemoved(event);
|
|
859 |
| } |
|
860 |
| } |
|
861 |
| } |
|
862 |
| } |
|
863 |
| |
|
864 |
| |
|
865 |
| |
|
866 |
| |
|
867 |
| |
|
868 |
| |
|
869 |
| |
|
870 |
| |
|
871 |
45
| private void dispatchCacheGroupEvent(CacheEntryEventType eventType, String group, String origin) {
|
|
872 |
45
| CacheGroupEvent event = new CacheGroupEvent(this, group, origin);
|
|
873 |
| |
|
874 |
| |
|
875 |
45
| Object[] listeners = listenerList.getListenerList();
|
|
876 |
| |
|
877 |
| |
|
878 |
| |
|
879 |
45
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
880 |
99
| if (listeners[i+1] instanceof CacheEntryEventListener) {
|
|
881 |
54
| CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i + 1];
|
|
882 |
54
| if (eventType.equals(CacheEntryEventType.GROUP_FLUSHED)) {
|
|
883 |
54
| listener.cacheGroupFlushed(event);
|
|
884 |
| } |
|
885 |
| } |
|
886 |
| } |
|
887 |
| } |
|
888 |
| |
|
889 |
| |
|
890 |
| |
|
891 |
| |
|
892 |
| |
|
893 |
| |
|
894 |
| |
|
895 |
| |
|
896 |
4565565
| private void dispatchCacheMapAccessEvent(CacheMapAccessEventType eventType, CacheEntry entry, String origin) {
|
|
897 |
4565565
| CacheMapAccessEvent event = new CacheMapAccessEvent(eventType, entry, origin);
|
|
898 |
| |
|
899 |
| |
|
900 |
4565565
| Object[] listeners = listenerList.getListenerList();
|
|
901 |
| |
|
902 |
| |
|
903 |
| |
|
904 |
4565565
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
905 |
597
| if (listeners[i+1] instanceof CacheMapAccessEventListener) {
|
|
906 |
347
| CacheMapAccessEventListener listener = (CacheMapAccessEventListener) listeners[i + 1];
|
|
907 |
347
| listener.accessed(event);
|
|
908 |
| } |
|
909 |
| } |
|
910 |
| } |
|
911 |
| |
|
912 |
| |
|
913 |
| |
|
914 |
| |
|
915 |
| |
|
916 |
| |
|
917 |
| |
|
918 |
| |
|
919 |
22
| private void dispatchCachePatternEvent(CacheEntryEventType eventType, String pattern, String origin) {
|
|
920 |
22
| CachePatternEvent event = new CachePatternEvent(this, pattern, origin);
|
|
921 |
| |
|
922 |
| |
|
923 |
22
| Object[] listeners = listenerList.getListenerList();
|
|
924 |
| |
|
925 |
| |
|
926 |
| |
|
927 |
22
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
928 |
46
| if (listeners[i+1] instanceof CacheEntryEventListener) {
|
|
929 |
26
| if (eventType.equals(CacheEntryEventType.PATTERN_FLUSHED)) {
|
|
930 |
26
| CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
|
|
931 |
26
| listener.cachePatternFlushed(event);
|
|
932 |
| } |
|
933 |
| } |
|
934 |
| } |
|
935 |
| } |
|
936 |
| |
|
937 |
| |
|
938 |
| |
|
939 |
| |
|
940 |
| |
|
941 |
| |
|
942 |
| |
|
943 |
6
| private void dispatchCachewideEvent(CachewideEventType eventType, Date date, String origin) {
|
|
944 |
6
| CachewideEvent event = new CachewideEvent(this, date, origin);
|
|
945 |
| |
|
946 |
| |
|
947 |
6
| Object[] listeners = listenerList.getListenerList();
|
|
948 |
| |
|
949 |
| |
|
950 |
| |
|
951 |
6
| for (int i = listeners.length - 2; i >= 0; i -= 2) {
|
|
952 |
12
| if (listeners[i+1] instanceof CacheEntryEventListener) {
|
|
953 |
7
| if (eventType.equals(CachewideEventType.CACHE_FLUSHED)) {
|
|
954 |
7
| CacheEntryEventListener listener = (CacheEntryEventListener) listeners[i+1];
|
|
955 |
7
| listener.cacheFlushed(event);
|
|
956 |
| } |
|
957 |
| } |
|
958 |
| } |
|
959 |
| } |
|
960 |
| |
|
961 |
| |
|
962 |
| |
|
963 |
| |
|
964 |
| |
|
965 |
| |
|
966 |
| |
|
967 |
| |
|
968 |
84
| private void flushEntry(CacheEntry entry, String origin) {
|
|
969 |
84
| String key = entry.getKey();
|
|
970 |
| |
|
971 |
| |
|
972 |
84
| entry.flush();
|
|
973 |
| |
|
974 |
84
| if (!entry.isNew()) {
|
|
975 |
| |
|
976 |
79
| cacheMap.put(key, entry);
|
|
977 |
| } |
|
978 |
| |
|
979 |
| |
|
980 |
84
| if (listenerList.getListenerCount() > 0) {
|
|
981 |
80
| CacheEntryEvent event = new CacheEntryEvent(this, entry, origin);
|
|
982 |
80
| dispatchCacheEntryEvent(CacheEntryEventType.ENTRY_FLUSHED, event);
|
|
983 |
| } |
|
984 |
| } |
|
985 |
| |
|
986 |
| |
|
987 |
| |
|
988 |
| |
|
989 |
30
| public int getSize() {
|
|
990 |
30
| synchronized(cacheMap) {
|
|
991 |
30
| return cacheMap.size();
|
|
992 |
| } |
|
993 |
| } |
|
994 |
| |
|
995 |
| |
|
996 |
| |
|
997 |
| |
|
998 |
40
| protected int getNbUpdateState() {
|
|
999 |
40
| synchronized(updateStates) {
|
|
1000 |
40
| return updateStates.size();
|
|
1001 |
| } |
|
1002 |
| } |
|
1003 |
| |
|
1004 |
| |
|
1005 |
| |
|
1006 |
| |
|
1007 |
| |
|
1008 |
| |
|
1009 |
0
| public int getNbEntries() {
|
|
1010 |
0
| synchronized(cacheMap) {
|
|
1011 |
0
| return cacheMap.size();
|
|
1012 |
| } |
|
1013 |
| } |
|
1014 |
| } |