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