1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.cache.mbean;
35
36 import info.magnolia.cms.util.MBeanUtil;
37 import info.magnolia.commands.CommandsManager;
38 import info.magnolia.context.Context;
39 import info.magnolia.context.MgnlContext;
40 import info.magnolia.context.SimpleContext;
41 import info.magnolia.module.ModuleRegistry;
42 import info.magnolia.module.cache.Cache;
43 import info.magnolia.module.cache.CacheFactory;
44 import info.magnolia.module.cache.CacheModule;
45 import info.magnolia.module.cache.DefaultCacheKey;
46 import info.magnolia.module.cache.cachepolicy.Default;
47
48 import java.util.HashMap;
49 import java.util.Map;
50
51 import javax.inject.Inject;
52 import javax.inject.Singleton;
53
54 import org.apache.commons.chain.Command;
55
56
57
58
59
60
61 @Singleton
62 public class CacheMonitor implements CacheMonitorMBean {
63
64 private static CacheMonitor instance;
65
66 private final CommandsManager commandsManager;
67 private int start;
68 private int stop;
69 private Map<String, Integer> calls = new HashMap<String, Integer>();
70 private Map<String, Integer> caches = new HashMap<String, Integer>();
71 private Map<String, Integer> domains = new HashMap<String, Integer>();
72
73 @Inject
74 public CacheMonitor(CommandsManager commandsManager) {
75 MBeanUtil.registerMBean("CacheMonitor", this);
76 this.commandsManager = commandsManager;
77
78
79 instance = this;
80 }
81
82
83
84
85 private CacheFactory getCacheFactory() {
86 CacheFactory factory = ModuleRegistry.Factory.getInstance().getModuleInstance(CacheModule.class).getCacheFactory();
87 return factory;
88 }
89
90 private Command getCommand(String commandName) {
91 Command flush = commandsManager.getCommand("cache", commandName);
92 if (flush == null) {
93
94 commandsManager.reload();
95 flush = commandsManager.getCommand("cache", commandName);
96 }
97 if (flush == null) {
98 throw new RuntimeException("Failed to invoke cache " + commandName + " command");
99 }
100 return flush;
101 }
102
103
104
105
106 public static CacheMonitor getInstance() {
107 return instance;
108 }
109
110 public void countStart() {
111 start++;
112 }
113
114 public void stop() {
115 stop++;
116 }
117
118
119
120
121 public void logBehavior(String name) {
122 Integer count = calls.get(name);
123 calls.put(name, count == null ? 1 : ++count);
124 }
125
126 public void countFlush(String name) {
127 caches.put(name, caches.get(name) + 1);
128 }
129
130 public void addCache(String name) {
131 caches.put(name, 0);
132 }
133
134 public void logAccess(Object cacheKey) {
135 if (cacheKey == null || !(cacheKey instanceof DefaultCacheKey)) {
136 return;
137 }
138 DefaultCacheKey key = (DefaultCacheKey) cacheKey;
139 Integer count = this.domains.get(key.getDomain());
140 this.domains.put(key.getDomain(), count == null ? 1 : ++count);
141 }
142
143
144 @Override
145 public void flushAll() throws Exception {
146 Command flush = getCommand("flushAll");
147 flush.execute(MgnlContext.getSystemContext());
148 }
149
150 @Override
151 public void flushByUUID(String repository, String uuid) throws Exception {
152 Command flush = getCommand("flushByUUID");
153 Context ctx = new SimpleContext(MgnlContext.getSystemContext());
154 ctx.put("repository", repository);
155 ctx.put("uuid", uuid);
156 flush.execute(ctx);
157 }
158
159
160 @Override
161 public Map<String, Integer> getAll() {
162 return calls;
163 }
164
165 @Override
166 public int getHits() {
167 return calls.get("useCache");
168 }
169
170 @Override
171 public int getBypasses() {
172 return calls.get("bypass");
173 }
174
175 @Override
176 public int getPuts() {
177 return calls.get("store");
178 }
179
180 @Override
181 public int getStopCalls() {
182 return stop;
183 }
184
185 @Override
186 public int getStartCalls() {
187 return start;
188 }
189
190 @Override
191 public Map<String, Integer> getFlushes() {
192 return caches;
193 }
194
195 @Override
196 public Map<String, Integer> getDomainAccesses() {
197 return domains;
198 }
199
200 @Override
201 public int getCachedKeysCount() {
202 int count = 0;
203
204 CacheFactory factory = getCacheFactory();
205 for (String name : caches.keySet()) {
206
207 if (Default.UUID_KEY_MAP_KEY.equals(name)) {
208 continue;
209 }
210 Cache cache = factory.getCache(name);
211 count += cache.getSize();
212 }
213 return count;
214 }
215
216 @Override
217 public int getCachedUUIDsCount() {
218 CacheFactory factory = getCacheFactory();
219 Cache cache = factory.getCache(Default.UUID_KEY_MAP_KEY);
220 return cache.getSize();
221 }
222 }