1 |
|
package org.apache.torque.manager; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import java.io.Serializable; |
20 |
|
import java.util.HashMap; |
21 |
|
import java.util.Map; |
22 |
|
|
23 |
|
import org.apache.commons.pool.ObjectPool; |
24 |
|
import org.apache.commons.pool.impl.StackObjectPool; |
25 |
|
|
26 |
|
import org.apache.jcs.access.GroupCacheAccess; |
27 |
|
import org.apache.jcs.access.exception.CacheException; |
28 |
|
|
29 |
|
import org.apache.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
|
32 |
|
import org.apache.torque.TorqueException; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public class MethodResultCache |
42 |
|
{ |
43 |
|
private ObjectPool pool; |
44 |
|
private GroupCacheAccess jcsCache; |
45 |
|
private Map groups; |
46 |
|
|
47 |
|
|
48 |
0 |
private static Log log = LogFactory.getLog(MethodResultCache.class); |
49 |
|
|
50 |
|
public MethodResultCache(GroupCacheAccess cache) |
51 |
|
throws TorqueException |
52 |
0 |
{ |
53 |
0 |
this.jcsCache = cache; |
54 |
0 |
groups = new HashMap(); |
55 |
0 |
pool = new StackObjectPool(class="keyword">new MethodCacheKey.Factory(), 10000); |
56 |
0 |
} |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
protected MethodResultCache() |
64 |
0 |
{ |
65 |
0 |
} |
66 |
|
|
67 |
|
public void clear() |
68 |
|
{ |
69 |
0 |
if (jcsCache != null) |
70 |
|
{ |
71 |
|
try |
72 |
|
{ |
73 |
0 |
jcsCache.remove(); |
74 |
0 |
groups.clear(); |
75 |
|
} |
76 |
0 |
catch (CacheException ce) |
77 |
|
{ |
78 |
0 |
log.error(new TorqueException( |
79 |
|
"Could not clear cache due to internal JCS error.", ce)); |
80 |
0 |
} |
81 |
|
} |
82 |
0 |
} |
83 |
|
|
84 |
|
protected Object getImpl(MethodCacheKey key) |
85 |
|
{ |
86 |
0 |
Object result = null; |
87 |
0 |
if (jcsCache != null) |
88 |
|
{ |
89 |
0 |
synchronized (this) |
90 |
|
{ |
91 |
0 |
result = jcsCache.getFromGroup(key, key.getGroupKey()); |
92 |
0 |
} |
93 |
|
} |
94 |
|
|
95 |
0 |
if (result != null) |
96 |
|
{ |
97 |
0 |
if (log.isDebugEnabled()) |
98 |
|
{ |
99 |
0 |
log.debug("MethodResultCache saved expensive operation: " + key); |
100 |
|
} |
101 |
|
} |
102 |
0 |
return result; |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
protected Object putImpl(MethodCacheKey key, Object value) |
107 |
|
throws TorqueException |
108 |
|
{ |
109 |
|
|
110 |
0 |
String group = key.getGroupKey(); |
111 |
0 |
if (!groups.containsKey(group)) |
112 |
|
{ |
113 |
0 |
groups.put(group, null); |
114 |
|
} |
115 |
|
|
116 |
0 |
Object old = null; |
117 |
0 |
if (jcsCache != null) |
118 |
|
{ |
119 |
|
try |
120 |
|
{ |
121 |
0 |
synchronized (this) |
122 |
|
{ |
123 |
0 |
old = jcsCache.getFromGroup(key, group); |
124 |
0 |
jcsCache.putInGroup(key, group, value); |
125 |
0 |
} |
126 |
|
} |
127 |
0 |
catch (CacheException ce) |
128 |
|
{ |
129 |
0 |
throw new TorqueException |
130 |
|
("Could not cache due to internal JCS error", ce); |
131 |
0 |
} |
132 |
|
} |
133 |
0 |
return old; |
134 |
|
} |
135 |
|
|
136 |
|
protected Object removeImpl(MethodCacheKey key) |
137 |
|
throws TorqueException |
138 |
|
{ |
139 |
0 |
Object old = null; |
140 |
0 |
if (jcsCache != null) |
141 |
|
{ |
142 |
0 |
synchronized (this) |
143 |
|
{ |
144 |
0 |
old = jcsCache.getFromGroup(key, key.getGroupKey()); |
145 |
0 |
jcsCache.remove(key, key.getGroupKey()); |
146 |
0 |
} |
147 |
|
} |
148 |
0 |
return old; |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
public Object get(Serializable instanceOrClass, String method) |
153 |
|
{ |
154 |
0 |
Object result = null; |
155 |
0 |
if (jcsCache != null) |
156 |
|
{ |
157 |
|
try |
158 |
|
{ |
159 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
160 |
0 |
key.init(instanceOrClass, method); |
161 |
0 |
result = getImpl(key); |
162 |
|
try |
163 |
|
{ |
164 |
0 |
pool.returnObject(key); |
165 |
|
} |
166 |
0 |
catch (Exception e) |
167 |
|
{ |
168 |
0 |
log.warn( |
169 |
|
"Nonfatal error. Could not return key to pool", e); |
170 |
0 |
} |
171 |
|
} |
172 |
0 |
catch (Exception e) |
173 |
|
{ |
174 |
0 |
log.error("", e); |
175 |
0 |
} |
176 |
|
} |
177 |
0 |
return result; |
178 |
|
} |
179 |
|
|
180 |
|
public Object get(Serializable instanceOrClass, String method, |
181 |
|
Serializable arg1) |
182 |
|
{ |
183 |
0 |
Object result = null; |
184 |
0 |
if (jcsCache != null) |
185 |
|
{ |
186 |
|
try |
187 |
|
{ |
188 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
189 |
0 |
key.init(instanceOrClass, method, arg1); |
190 |
0 |
result = getImpl(key); |
191 |
|
try |
192 |
|
{ |
193 |
0 |
pool.returnObject(key); |
194 |
|
} |
195 |
0 |
catch (Exception e) |
196 |
|
{ |
197 |
0 |
log.warn( |
198 |
|
"Nonfatal error. Could not return key to pool", e); |
199 |
0 |
} |
200 |
|
} |
201 |
0 |
catch (Exception e) |
202 |
|
{ |
203 |
0 |
log.error("", e); |
204 |
0 |
} |
205 |
|
} |
206 |
0 |
return result; |
207 |
|
} |
208 |
|
|
209 |
|
public Object get(Serializable instanceOrClass, String method, |
210 |
|
Serializable arg1, Serializable arg2) |
211 |
|
{ |
212 |
0 |
Object result = null; |
213 |
0 |
if (jcsCache != null) |
214 |
|
{ |
215 |
|
try |
216 |
|
{ |
217 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
218 |
0 |
key.init(instanceOrClass, method, arg1, arg2); |
219 |
0 |
result = getImpl(key); |
220 |
|
try |
221 |
|
{ |
222 |
0 |
pool.returnObject(key); |
223 |
|
} |
224 |
0 |
catch (Exception e) |
225 |
|
{ |
226 |
0 |
log.warn( |
227 |
|
"Nonfatal error. Could not return key to pool", e); |
228 |
0 |
} |
229 |
|
} |
230 |
0 |
catch (Exception e) |
231 |
|
{ |
232 |
0 |
log.error("", e); |
233 |
0 |
} |
234 |
|
} |
235 |
0 |
return result; |
236 |
|
} |
237 |
|
|
238 |
|
public Object get(Serializable instanceOrClass, String method, |
239 |
|
Serializable arg1, Serializable arg2, |
240 |
|
Serializable arg3) |
241 |
|
{ |
242 |
0 |
Object result = null; |
243 |
0 |
if (jcsCache != null) |
244 |
|
{ |
245 |
|
try |
246 |
|
{ |
247 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
248 |
0 |
key.init(instanceOrClass, method, arg1, arg2, arg3); |
249 |
0 |
result = getImpl(key); |
250 |
|
try |
251 |
|
{ |
252 |
0 |
pool.returnObject(key); |
253 |
|
} |
254 |
0 |
catch (Exception e) |
255 |
|
{ |
256 |
0 |
log.warn( |
257 |
|
"Nonfatal error. Could not return key to pool", e); |
258 |
0 |
} |
259 |
|
} |
260 |
0 |
catch (Exception e) |
261 |
|
{ |
262 |
0 |
log.error("", e); |
263 |
0 |
} |
264 |
|
} |
265 |
0 |
return result; |
266 |
|
} |
267 |
|
|
268 |
|
public Object get(Serializable[] keys) |
269 |
|
{ |
270 |
0 |
Object result = null; |
271 |
0 |
if (jcsCache != null) |
272 |
|
{ |
273 |
|
try |
274 |
|
{ |
275 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
276 |
0 |
key.init(keys); |
277 |
0 |
result = getImpl(key); |
278 |
|
try |
279 |
|
{ |
280 |
0 |
pool.returnObject(key); |
281 |
|
} |
282 |
0 |
catch (Exception e) |
283 |
|
{ |
284 |
0 |
log.warn( |
285 |
|
"Nonfatal error. Could not return key to pool", e); |
286 |
0 |
} |
287 |
|
} |
288 |
0 |
catch (Exception e) |
289 |
|
{ |
290 |
0 |
log.error("", e); |
291 |
0 |
} |
292 |
|
} |
293 |
0 |
return result; |
294 |
|
} |
295 |
|
|
296 |
|
public void put(Object value, Serializable instanceOrClass, String method) |
297 |
|
{ |
298 |
|
try |
299 |
|
{ |
300 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
301 |
0 |
key.init(instanceOrClass, method); |
302 |
0 |
putImpl(key, value); |
303 |
|
} |
304 |
0 |
catch (Exception e) |
305 |
|
{ |
306 |
0 |
log.error("", e); |
307 |
0 |
} |
308 |
0 |
} |
309 |
|
|
310 |
|
public void put(Object value, Serializable instanceOrClass, |
311 |
|
String method, Serializable arg1) |
312 |
|
{ |
313 |
|
try |
314 |
|
{ |
315 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
316 |
0 |
key.init(instanceOrClass, method, arg1); |
317 |
0 |
putImpl(key, value); |
318 |
|
} |
319 |
0 |
catch (Exception e) |
320 |
|
{ |
321 |
0 |
log.error("", e); |
322 |
0 |
} |
323 |
0 |
} |
324 |
|
|
325 |
|
public void put(Object value, Serializable instanceOrClass, String method, |
326 |
|
Serializable arg1, Serializable arg2) |
327 |
|
{ |
328 |
|
try |
329 |
|
{ |
330 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
331 |
0 |
key.init(instanceOrClass, method, arg1, arg2); |
332 |
0 |
putImpl(key, value); |
333 |
|
} |
334 |
0 |
catch (Exception e) |
335 |
|
{ |
336 |
0 |
log.error("", e); |
337 |
0 |
} |
338 |
0 |
} |
339 |
|
|
340 |
|
public void put(Object value, Serializable instanceOrClass, String method, |
341 |
|
Serializable arg1, Serializable arg2, Serializable arg3) |
342 |
|
{ |
343 |
|
try |
344 |
|
{ |
345 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
346 |
0 |
key.init(instanceOrClass, method, arg1, arg2, arg3); |
347 |
0 |
putImpl(key, value); |
348 |
|
} |
349 |
0 |
catch (Exception e) |
350 |
|
{ |
351 |
0 |
log.error("", e); |
352 |
0 |
} |
353 |
0 |
} |
354 |
|
|
355 |
|
public void put(Object value, Serializable[] keys) |
356 |
|
{ |
357 |
|
try |
358 |
|
{ |
359 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
360 |
0 |
key.init(keys); |
361 |
0 |
putImpl(key, value); |
362 |
|
} |
363 |
0 |
catch (Exception e) |
364 |
|
{ |
365 |
0 |
log.error("", e); |
366 |
0 |
} |
367 |
0 |
} |
368 |
|
|
369 |
|
|
370 |
|
public void removeAll(Serializable instanceOrClass, String method) |
371 |
|
{ |
372 |
0 |
if (jcsCache != null) |
373 |
|
{ |
374 |
|
try |
375 |
|
{ |
376 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
377 |
0 |
key.init(instanceOrClass, method); |
378 |
0 |
String groupName = key.getGroupKey(); |
379 |
0 |
jcsCache.invalidateGroup(groupName); |
380 |
0 |
groups.remove(groupName); |
381 |
|
try |
382 |
|
{ |
383 |
0 |
pool.returnObject(key); |
384 |
|
} |
385 |
0 |
catch (Exception e) |
386 |
|
{ |
387 |
0 |
log.warn( |
388 |
|
"Nonfatal error. Could not return key to pool", e); |
389 |
0 |
} |
390 |
|
} |
391 |
0 |
catch (Exception e) |
392 |
|
{ |
393 |
0 |
log.error("", e); |
394 |
0 |
} |
395 |
|
} |
396 |
0 |
} |
397 |
|
|
398 |
|
|
399 |
|
public Object remove(Serializable instanceOrClass, String method) |
400 |
|
{ |
401 |
0 |
Object result = null; |
402 |
0 |
if (jcsCache != null) |
403 |
|
{ |
404 |
|
try |
405 |
|
{ |
406 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
407 |
0 |
key.init(instanceOrClass, method); |
408 |
0 |
result = removeImpl(key); |
409 |
|
try |
410 |
|
{ |
411 |
0 |
pool.returnObject(key); |
412 |
|
} |
413 |
0 |
catch (Exception e) |
414 |
|
{ |
415 |
0 |
log.warn( |
416 |
|
"Nonfatal error. Could not return key to pool", e); |
417 |
0 |
} |
418 |
|
} |
419 |
0 |
catch (Exception e) |
420 |
|
{ |
421 |
0 |
log.error("", e); |
422 |
0 |
} |
423 |
|
} |
424 |
0 |
return result; |
425 |
|
} |
426 |
|
|
427 |
|
public Object remove(Serializable instanceOrClass, String method, |
428 |
|
Serializable arg1) |
429 |
|
{ |
430 |
0 |
Object result = null; |
431 |
0 |
if (jcsCache != null) |
432 |
|
{ |
433 |
|
try |
434 |
|
{ |
435 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
436 |
0 |
key.init(instanceOrClass, method, arg1); |
437 |
0 |
result = removeImpl(key); |
438 |
|
try |
439 |
|
{ |
440 |
0 |
pool.returnObject(key); |
441 |
|
} |
442 |
0 |
catch (Exception e) |
443 |
|
{ |
444 |
0 |
log.warn( |
445 |
|
"Nonfatal error. Could not return key to pool", e); |
446 |
0 |
} |
447 |
|
} |
448 |
0 |
catch (Exception e) |
449 |
|
{ |
450 |
0 |
log.error("Error removing element", e); |
451 |
0 |
} |
452 |
|
} |
453 |
0 |
return result; |
454 |
|
} |
455 |
|
|
456 |
|
public Object remove(Serializable instanceOrClass, String method, |
457 |
|
Serializable arg1, Serializable arg2) |
458 |
|
{ |
459 |
0 |
Object result = null; |
460 |
0 |
if (jcsCache != null) |
461 |
|
{ |
462 |
|
try |
463 |
|
{ |
464 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
465 |
0 |
key.init(instanceOrClass, method, arg1, arg2); |
466 |
0 |
result = removeImpl(key); |
467 |
|
try |
468 |
|
{ |
469 |
0 |
pool.returnObject(key); |
470 |
|
} |
471 |
0 |
catch (Exception e) |
472 |
|
{ |
473 |
0 |
log.warn( |
474 |
|
"Nonfatal error: Could not return key to pool", e); |
475 |
0 |
} |
476 |
|
} |
477 |
0 |
catch (Exception e) |
478 |
|
{ |
479 |
0 |
log.error("Error removing element from cache", e); |
480 |
0 |
} |
481 |
|
} |
482 |
0 |
return result; |
483 |
|
} |
484 |
|
|
485 |
|
public Object remove(Serializable instanceOrClass, String method, |
486 |
|
Serializable arg1, Serializable arg2, |
487 |
|
Serializable arg3) |
488 |
|
{ |
489 |
0 |
Object result = null; |
490 |
0 |
if (jcsCache != null) |
491 |
|
{ |
492 |
|
try |
493 |
|
{ |
494 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
495 |
0 |
key.init(instanceOrClass, method, arg1, arg2, arg3); |
496 |
0 |
result = removeImpl(key); |
497 |
|
try |
498 |
|
{ |
499 |
0 |
pool.returnObject(key); |
500 |
|
} |
501 |
0 |
catch (Exception e) |
502 |
|
{ |
503 |
0 |
log.warn( |
504 |
|
"Nonfatal error. Could not return key to pool", e); |
505 |
0 |
} |
506 |
|
} |
507 |
0 |
catch (Exception e) |
508 |
|
{ |
509 |
0 |
log.error("Error removing element from cache", e); |
510 |
0 |
} |
511 |
|
} |
512 |
0 |
return result; |
513 |
|
} |
514 |
|
|
515 |
|
public Object remove(Serializable[] keys) |
516 |
|
{ |
517 |
0 |
Object result = null; |
518 |
0 |
if (jcsCache != null) |
519 |
|
{ |
520 |
|
try |
521 |
|
{ |
522 |
0 |
MethodCacheKey key = (MethodCacheKey) pool.borrowObject(); |
523 |
0 |
key.init(keys); |
524 |
0 |
result = removeImpl(key); |
525 |
|
try |
526 |
|
{ |
527 |
0 |
pool.returnObject(key); |
528 |
|
} |
529 |
0 |
catch (Exception e) |
530 |
|
{ |
531 |
0 |
log.warn( |
532 |
|
"Nonfatal error: Could not return key to pool", e); |
533 |
0 |
} |
534 |
|
} |
535 |
0 |
catch (Exception e) |
536 |
|
{ |
537 |
0 |
log.error("Error removing element from cache", e); |
538 |
0 |
} |
539 |
|
} |
540 |
0 |
return result; |
541 |
|
} |
542 |
|
} |