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