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