1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.util.spring; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Arrays; |
20 | |
import java.util.List; |
21 | |
|
22 | |
import net.sf.ehcache.CacheManager; |
23 | |
import net.sf.ehcache.Element; |
24 | |
import net.sf.ehcache.ObjectExistsException; |
25 | |
|
26 | |
import org.aopalliance.aop.Advice; |
27 | |
import org.apache.commons.collections.keyvalue.MultiKey; |
28 | |
import org.apache.log4j.Logger; |
29 | |
import org.aspectj.lang.ProceedingJoinPoint; |
30 | |
|
31 | |
public class MethodArgsToObjectEhcacheAdvice implements Advice { |
32 | 0 | final Logger LOG = Logger.getLogger(getClass()); |
33 | |
|
34 | |
private CacheManager cacheManager; |
35 | |
private String cacheName; |
36 | |
private boolean enabled; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public MethodArgsToObjectEhcacheAdvice() { |
42 | 0 | super(); |
43 | 0 | } |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public MethodArgsToObjectEhcacheAdvice(String cacheName) { |
49 | 0 | super(); |
50 | 0 | this.cacheName = cacheName; |
51 | 0 | } |
52 | |
|
53 | |
public Object invalidateCache(ProceedingJoinPoint pjp) throws Throwable { |
54 | 0 | Object result = pjp.proceed(); |
55 | 0 | if(enabled){ |
56 | 0 | if (cacheManager == null) { |
57 | 0 | cacheManager = CacheManager.getInstance(); |
58 | |
try { |
59 | 0 | cacheManager.addCache(cacheName); |
60 | 0 | } catch (ObjectExistsException e) { |
61 | |
|
62 | 0 | } |
63 | |
} |
64 | 0 | LOG.info("Invalidating Cache: " + cacheName); |
65 | 0 | cacheManager.getCache(cacheName).removeAll(); |
66 | |
} |
67 | 0 | return result; |
68 | |
} |
69 | |
|
70 | |
public Object getFromCache(ProceedingJoinPoint pjp) throws Throwable { |
71 | 0 | if(!enabled){ |
72 | 0 | return pjp.proceed(); |
73 | |
} |
74 | |
|
75 | 0 | if (cacheManager == null) { |
76 | 0 | cacheManager = CacheManager.getInstance(); |
77 | |
try { |
78 | 0 | cacheManager.addCache(cacheName); |
79 | 0 | } catch (ObjectExistsException e) { |
80 | |
|
81 | 0 | } |
82 | |
} |
83 | 0 | MultiKey cacheKey = getCacheKey(pjp); |
84 | |
|
85 | 0 | Element cachedResult = cacheManager.getCache(cacheName).get(cacheKey); |
86 | 0 | Object result = null; |
87 | 0 | if (cachedResult == null) { |
88 | 0 | result = pjp.proceed(); |
89 | 0 | LOG.info("Storing to Cache: " + cacheName); |
90 | 0 | cacheManager.getCache(cacheName).put(new Element(cacheKey, result)); |
91 | |
} else { |
92 | 0 | LOG.info("Found in Cache: " + cacheName); |
93 | 0 | result = cachedResult.getValue(); |
94 | |
} |
95 | |
|
96 | 0 | return result; |
97 | |
} |
98 | |
|
99 | |
private MultiKey getCacheKey(ProceedingJoinPoint pjp) { |
100 | 0 | List<Object> keyList = new ArrayList<Object>(); |
101 | 0 | keyList.add(pjp.getSignature().getName()); |
102 | 0 | for(Object arg : pjp.getArgs()){ |
103 | 0 | keyList.add(arg.toString()); |
104 | |
} |
105 | 0 | return new MultiKey(keyList.toArray()); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public String getCacheName() { |
112 | 0 | return cacheName; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public void setCacheName(String cacheName) { |
120 | 0 | this.cacheName = cacheName; |
121 | 0 | } |
122 | |
|
123 | |
public void setCacheManager(CacheManager cacheManager) { |
124 | 0 | this.cacheManager = cacheManager; |
125 | 0 | } |
126 | |
|
127 | |
public boolean isEnabled() { |
128 | 0 | return enabled; |
129 | |
} |
130 | |
|
131 | |
public void setEnabled(boolean enabled) { |
132 | 0 | this.enabled = enabled; |
133 | 0 | } |
134 | |
|
135 | |
} |