1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util.cache;
17
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import com.opensymphony.oscache.base.events.CacheEntryEvent;
25 import com.opensymphony.oscache.base.events.CacheEntryEventListener;
26 import com.opensymphony.oscache.base.events.CacheGroupEvent;
27 import com.opensymphony.oscache.base.events.CachePatternEvent;
28 import com.opensymphony.oscache.base.events.CachewideEvent;
29
30
31
32
33
34 @SuppressWarnings("unchecked")
35 abstract public class OSCacheMonitor implements CacheEntryEventListener {
36 private static final Log LOG = LogFactory.getLog(OSCacheMonitor.class);
37
38 private final String purpose;
39 private final Set entries;
40
41
42
43
44
45
46 public OSCacheMonitor(String purpose) {
47 this.purpose = purpose;
48 entries = new HashSet();
49
50 if ( LOG.isInfoEnabled() ) {
51 LOG.info("created " + purpose + " CacheMonitor ");
52 }
53 }
54
55
56 public void cacheEntryAdded(CacheEntryEvent event) {
57 entries.add(event.getKey());
58 if ( LOG.isDebugEnabled() ) {
59 logEntryEvent("added", event);
60 }
61 }
62
63 public void cacheEntryUpdated(CacheEntryEvent event) {
64 if ( LOG.isDebugEnabled() ) {
65 logEntryEvent("updated", event);
66 }
67 }
68
69 public void cacheEntryFlushed(CacheEntryEvent event) {
70 entries.remove(event.getKey());
71 if ( LOG.isDebugEnabled() ) {
72 logEntryEvent("flushed", event);
73 }
74 }
75
76 public void cacheEntryRemoved(CacheEntryEvent event) {
77 entries.remove(event.getKey());
78 if ( LOG.isDebugEnabled() ) {
79 logEntryEvent("removed", event);
80 }
81 }
82
83 public void cacheFlushed(CachewideEvent event) {
84 entries.clear();
85 if ( LOG.isDebugEnabled() ) {
86 LOG.debug(purpose + " flushed cache (" + entries.size() + " entries)");
87 }
88 }
89
90
91 public void cacheGroupFlushed(CacheGroupEvent event) {
92
93 if ( LOG.isInfoEnabled() ) {
94 LOG.info(purpose + " flushed cache group '" + event.getGroup() + "'");
95 }
96 }
97
98 public void cachePatternFlushed(CachePatternEvent event) {
99
100 if ( LOG.isInfoEnabled() ) {
101 LOG.info(purpose + " flushed cache pattern '" + event.getPattern() + "'");
102 }
103 }
104
105 private void logEntryEvent(String verb, CacheEntryEvent event) {
106 LOG.debug(purpose + " " + verb + " entry '" + event.getKey() + "' (" + entries.size() + " entries)");
107 }
108 }