Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MethodCacheInterceptor |
|
| 3.888888888888889;3.889 |
1 | /* | |
2 | * Copyright 2002-2004 the original author or authors. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | * | |
16 | * Code obtained from http://opensource.atlassian.com/confluence/spring/display/DISC/Caching+the+result+of+methods+using+Spring+and+EHCache | |
17 | * and modified for use within Kuali | |
18 | */ | |
19 | ||
20 | // begin Kuali Foundation modification | |
21 | package org.kuali.rice.kns.util.cache; | |
22 | ||
23 | // Kuali Foundation modification: changed some imports | |
24 | import java.io.Serializable; | |
25 | ||
26 | import org.aopalliance.intercept.MethodInterceptor; | |
27 | import org.aopalliance.intercept.MethodInvocation; | |
28 | import org.apache.commons.logging.Log; | |
29 | import org.apache.commons.logging.LogFactory; | |
30 | import org.springframework.beans.factory.InitializingBean; | |
31 | import org.springframework.util.Assert; | |
32 | ||
33 | import com.opensymphony.oscache.base.Cache; | |
34 | import com.opensymphony.oscache.base.NeedsRefreshException; | |
35 | ||
36 | /** | |
37 | * begin Kuali Foundation modification | |
38 | * This class implements org.aopalliance.intercept.MethodInterceptor. This interceptor builds the cache key for the method and | |
39 | * checks if an earlier result was cached with that key. If so, the cached result is returned; otherwise, the intercepted method is | |
40 | * called and the result cached for future use. | |
41 | * end Kuali Foundation modification | |
42 | * | |
43 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
44 | * @since 2004.10.07 | |
45 | */ | |
46 | 0 | public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean { |
47 | 0 | private static final Log LOG = LogFactory.getLog(MethodCacheInterceptor.class); |
48 | ||
49 | private Cache cache; | |
50 | // begin Kuali Foundation modification | |
51 | 0 | private int expirationTimeInSeconds = 1000; |
52 | 0 | private long maxEntrySizeInBytes = 0; |
53 | // end Kuali Foundation modification | |
54 | ||
55 | /** | |
56 | * begin Kuali Foundation modification | |
57 | * @param cache name of cache to be used | |
58 | * end Kuali Foundation modification | |
59 | */ | |
60 | public void setCache(Cache cache) { | |
61 | 0 | this.cache = cache; |
62 | 0 | } |
63 | ||
64 | // begin Kuali Foundation modification | |
65 | /** | |
66 | * Entries older than this will have their contents replaced by the return value from a call to the appropriate method | |
67 | * | |
68 | * @param expirationTimeInSeconds | |
69 | */ | |
70 | public void setExpirationTimeInSeconds(int expirationTimeInSeconds) { | |
71 | 0 | this.expirationTimeInSeconds = expirationTimeInSeconds; |
72 | 0 | } |
73 | ||
74 | /** | |
75 | * Entries whose size is larger than the current value will not be cached. If the maxEntrySizeInBytes <= 0, no size limit will | |
76 | * be applied. | |
77 | * | |
78 | * @param maxEntrySizeInBytes | |
79 | */ | |
80 | public void setMaxEntrySizeInBytes(long maxEntrySizeInBytes) { | |
81 | 0 | this.maxEntrySizeInBytes = maxEntrySizeInBytes; |
82 | 0 | } |
83 | // end Kuali Foundation modification | |
84 | ||
85 | ||
86 | /** | |
87 | * Checks if required attributes are provided. | |
88 | * | |
89 | * begin Kuali Foundation modification | |
90 | * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() | |
91 | * end Kuali Foundation modification | |
92 | */ | |
93 | public void afterPropertiesSet() throws Exception { | |
94 | 0 | Assert.notNull(cache, "A cache is required. Use setCache(Cache) to provide one."); |
95 | 0 | } |
96 | ||
97 | /** | |
98 | * begin Kuali Foundation modification | |
99 | * Caches method results, if possible. | |
100 | * <p> | |
101 | * Results must be Serializable to be cached. Method with unSerializable results will never have their results cached, and will | |
102 | * log error messages complaining about that fact. | |
103 | * | |
104 | * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation) | |
105 | * end Kuali Foundation modification | |
106 | */ | |
107 | public Object invoke(MethodInvocation invocation) throws Throwable { | |
108 | // begin Kuali Foundation modification | |
109 | 0 | boolean cancelUpdate = true; |
110 | ||
111 | 0 | Object methodResult = null; |
112 | 0 | String cacheKey = buildCacheKey(invocation); |
113 | ||
114 | // lookup result in cache | |
115 | 0 | if (LOG.isTraceEnabled()) { |
116 | 0 | LOG.trace("looking for method result for invocation '" + cacheKey + "'"); |
117 | } | |
118 | try { | |
119 | 0 | CopiedObject cachedEntry = (CopiedObject) cache.getFromCache(cacheKey, expirationTimeInSeconds); |
120 | 0 | if (LOG.isDebugEnabled()) { |
121 | 0 | LOG.debug("using cached result deepCopy for invocation '" + cacheKey + "'"); |
122 | } | |
123 | ||
124 | // really expensive hack to try to keep from returning direct references to modifiable cached values | |
125 | // because if you return a direct reference to a cached value to someone, and it is of a mutable type, any changes they | |
126 | // make to what seems like "their copy" will also be reflected in the cached value, which is a Really Bad Thing | |
127 | 0 | methodResult = cachedEntry.getContent(); // getContent returns a copy |
128 | 0 | cancelUpdate = false; |
129 | 0 | } |
130 | 0 | catch (NeedsRefreshException e) { |
131 | // call intercepted method | |
132 | try { | |
133 | 0 | if (LOG.isTraceEnabled()) { |
134 | 0 | LOG.trace("calling intercepted method for invocation '" + cacheKey + "'"); |
135 | } | |
136 | 0 | methodResult = invocation.proceed(); |
137 | } | |
138 | 0 | catch (Exception invocationException) { |
139 | 0 | LOG.warn("unable to cache methodResult: caught exception invoking intercepted method: '" + invocationException); |
140 | 0 | throw invocationException; |
141 | 0 | } |
142 | ||
143 | // cache method result, if possible | |
144 | // there's no way to tell whether a result is cacheable until after the method gets called, | |
145 | // since methods may hand back a Serializable object even if the method is declared to return | |
146 | // a more general nonSerializable type (e.g. List is not Serializable, but if the method | |
147 | // actually returns ArrayList instances, they are) | |
148 | // | |
149 | // nulls are a special case, since isAssignableFrom will never return true, yet they are | |
150 | // Serializable (at least for this kind of use) | |
151 | // | |
152 | // caching a deepCopy of the methodResult to prevent someone changing the cached value directly, | |
153 | // through a shared reference | |
154 | 0 | if ((methodResult == null) || (Serializable.class.isAssignableFrom(methodResult.getClass()))) { |
155 | try { | |
156 | 0 | CopiedObject oldContent = (CopiedObject) e.getCacheContent(); |
157 | ||
158 | 0 | CopiedObject newContent = oldContent; |
159 | 0 | if ( newContent == null ) { |
160 | 0 | newContent = new CopiedObject(); |
161 | } | |
162 | 0 | newContent.setContent((Serializable)methodResult); |
163 | ||
164 | // if no size limit, or under size limit, add to cache | |
165 | 0 | if ((maxEntrySizeInBytes <= 0) || (newContent.getSize() <= maxEntrySizeInBytes)) { |
166 | 0 | if (LOG.isTraceEnabled()) { |
167 | 0 | LOG.trace("caching results for invocation '" + cacheKey + "'"); |
168 | } | |
169 | 0 | cache.putInCache(cacheKey, newContent); |
170 | ||
171 | // adding, not updating | |
172 | 0 | cancelUpdate = false; |
173 | } | |
174 | else { | |
175 | 0 | if (LOG.isTraceEnabled()) { |
176 | 0 | LOG.trace("rejecting oversized methodResult (" + newContent.getSize() + " bytes) for invocation '" + cacheKey + "'"); |
177 | } | |
178 | ||
179 | // size limit exceeded: remove existing (expired) cache entry, if any | |
180 | 0 | if (oldContent != null) { |
181 | 0 | if (LOG.isTraceEnabled()) { |
182 | 0 | LOG.trace("flushing previous value for oversized invocation '" + cacheKey + "'"); |
183 | } | |
184 | ||
185 | 0 | cache.cancelUpdate(cacheKey); |
186 | 0 | cache.flushEntry(cacheKey); |
187 | ||
188 | // already canceled the update, don't need to cancel the update again | |
189 | 0 | cancelUpdate = false; |
190 | } | |
191 | } | |
192 | } | |
193 | 0 | catch (Exception cacheException) { |
194 | 0 | LOG.error("unable to cache methodResult: caught exception invoking putInCache: '" + cacheException); |
195 | 0 | throw cacheException; |
196 | 0 | } |
197 | } | |
198 | else { | |
199 | 0 | LOG.error("unable to cache nonSerializable result type for invocation '" + cacheKey + "'"); |
200 | } | |
201 | 0 | } |
202 | finally { | |
203 | // it is imperative that you call cancelUpdate if you aren't going to update the cache entry | |
204 | 0 | if (cancelUpdate) { |
205 | 0 | cache.cancelUpdate(cacheKey); |
206 | } | |
207 | 0 | } |
208 | ||
209 | ||
210 | 0 | return methodResult; |
211 | // end Kuali Foundation modification | |
212 | } | |
213 | ||
214 | // begin Kuali Foundation modification | |
215 | /** | |
216 | * @param invocation MethodInvocation being handled | |
217 | * @return cache key: className.methodName(paramClass=argValue[,paramClass=argValue...]) | |
218 | */ | |
219 | private String buildCacheKey(MethodInvocation invocation) { | |
220 | 0 | return buildCacheKey(invocation.getStaticPart().toString(), invocation.getArguments()); |
221 | } | |
222 | ||
223 | ||
224 | /** | |
225 | * @param className | |
226 | * @param methodName | |
227 | * @param paramTypes | |
228 | * @param argValues | |
229 | * @return cache key: className.methodName(paramClass=argValue[,paramClass=argValue...]) | |
230 | */ | |
231 | public String buildCacheKey(String methodSignature, Object[] argValues) { | |
232 | 0 | StringBuffer cacheKey = new StringBuffer(methodSignature); |
233 | 0 | cacheKey.append(": "); |
234 | 0 | if (argValues != null) { |
235 | 0 | for (int i = 0; i < argValues.length; i++) { |
236 | 0 | if (i > 0) { |
237 | 0 | cacheKey.append(","); |
238 | } | |
239 | // handle weird cache bug: | |
240 | // if you call a one-arg method foo with a null arg i.e. foo(null), | |
241 | // and then call it with an argument whose toString evaluates to "null", | |
242 | // OSCache gets stuck in an infinite wait() call because it somehow thinks | |
243 | // another thread is already updating this cache entry | |
244 | // | |
245 | // workaround: change so that args which are actually null literal have | |
246 | // some weird, unlikely-to-be-encountered String representation | |
247 | 0 | if (argValues[i] == null) { |
248 | 0 | cacheKey.append("<literal null>"); |
249 | } | |
250 | else { | |
251 | 0 | cacheKey.append(argValues[i]); |
252 | } | |
253 | } | |
254 | } | |
255 | 0 | return cacheKey.toString(); |
256 | } | |
257 | ||
258 | ||
259 | /** | |
260 | * @param key | |
261 | * @return true if the cache contains an entry with the given key | |
262 | */ | |
263 | public boolean containsCacheKey(String key) { | |
264 | 0 | boolean contains = false; |
265 | ||
266 | try { | |
267 | 0 | cache.getFromCache(key); |
268 | 0 | contains = true; |
269 | } | |
270 | 0 | catch (NeedsRefreshException e) { |
271 | // it is imperative that you call cancelUpdate if you aren't going to update the cache entry that caused the | |
272 | // NeedsRefreshException above | |
273 | 0 | cache.cancelUpdate(key); |
274 | 0 | contains = false; |
275 | 0 | } |
276 | ||
277 | 0 | return contains; |
278 | } | |
279 | /** | |
280 | * Removes a method cache if one exists for the given key. | |
281 | * @param cacheKey - key for method signature and parameters - see buildCacheKey | |
282 | */ | |
283 | public void removeCacheKey(String cacheKey) { | |
284 | 0 | if (!containsCacheKey(cacheKey)) { |
285 | 0 | return; |
286 | } | |
287 | ||
288 | 0 | if ( LOG.isDebugEnabled() ) { |
289 | 0 | LOG.debug("removing method cache for key: " + cacheKey); |
290 | } | |
291 | 0 | cache.cancelUpdate(cacheKey); |
292 | 0 | cache.flushEntry(cacheKey); |
293 | 0 | } |
294 | ||
295 | // Kuali Foundation modification: removed getCacheKey(String, String, Object[]) | |
296 | // end Kuali Foundation modification | |
297 | } |