Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

GPCACHE-7 updated caching so controller method calls are excluded

  • Loading branch information...
commit 6e503e4e7a4bf07646392c7842c9c95d804f9f4b 1 parent 2568383
Burt Beckwith authored
View
8 CacheGrailsPlugin.groovy
@@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+import grails.plugin.cache.CacheBeanPostProcessor
import grails.plugin.cache.CacheConfigArtefactHandler
import grails.plugin.cache.ConfigLoader
import grails.plugin.cache.GrailsConcurrentMapCacheManager
@@ -115,10 +116,17 @@ class CacheGrailsPlugin {
if (!(order instanceof Number)) order = Ordered.LOWEST_PRECEDENCE
xmlns cache: 'http://www.springframework.org/schema/cache'
+
+ // creates 3 beans: org.springframework.cache.config.internalCacheAdvisor (org.springframework.cache.interceptor.BeanFactoryCacheOperationSourceAdvisor),
+ // org.springframework.cache.annotation.AnnotationCacheOperationSource#0 (org.springframework.cache.annotation.AnnotationCacheOperationSource),
+ // org.springframework.cache.interceptor.CacheInterceptor#0 (org.springframework.cache.interceptor.CacheInterceptor)
cache.'annotation-driven'('cache-manager': 'grailsCacheManager',
mode: 'proxy', order: order,
'proxy-target-class': proxyTargetClass)
+ // updates the AnnotationCacheOperationSource with a custom subclass
+ cacheBeanPostProcessor(CacheBeanPostProcessor)
+
grailsCacheManager(GrailsConcurrentMapCacheManager)
grailsCacheConfigLoader(ConfigLoader)
View
60 src/java/grails/plugin/cache/CacheBeanPostProcessor.java
@@ -0,0 +1,60 @@
+/* Copyright 2012 SpringSource.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package grails.plugin.cache;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.MutablePropertyValues;
+import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
+import org.springframework.beans.factory.config.RuntimeBeanReference;
+import org.springframework.beans.factory.support.AbstractBeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionRegistry;
+import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
+
+/**
+ * Changes the bean class of the org.springframework.cache.annotation.AnnotationCacheOperationSource#0
+ * bean to a custom subclass.
+ *
+ * @author Burt Beckwith
+ */
+public class CacheBeanPostProcessor implements BeanDefinitionRegistryPostProcessor {
+
+ protected Logger log = LoggerFactory.getLogger(getClass());
+
+ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
+ log.info("postProcessBeanDefinitionRegistry start");
+
+ AbstractBeanDefinition beanDef = (AbstractBeanDefinition)registry.getBeanDefinition(
+ "org.springframework.cache.annotation.AnnotationCacheOperationSource#0");
+
+ // change the class to the plugin's subclass
+ beanDef.setBeanClass(GrailsAnnotationCacheOperationSource.class);
+
+ // wire in the dependency for the grailsApplication
+ MutablePropertyValues props = beanDef.getPropertyValues();
+ if (props == null) {
+ props = new MutablePropertyValues();
+ beanDef.setPropertyValues(props);
+ }
+ props.addPropertyValue("grailsApplication", new RuntimeBeanReference("grailsApplication", true));
+
+ log.debug("updated {}", beanDef);
+ }
+
+ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
+ log.info("postProcessBeanFactory");
+ }
+}
View
74 src/java/grails/plugin/cache/GrailsAnnotationCacheOperationSource.java
@@ -0,0 +1,74 @@
+/* Copyright 2012 SpringSource.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package grails.plugin.cache;
+
+import java.lang.reflect.Method;
+import java.util.Collection;
+
+import org.codehaus.groovy.grails.commons.ControllerArtefactHandler;
+import org.codehaus.groovy.grails.commons.GrailsApplication;
+import org.springframework.cache.annotation.AnnotationCacheOperationSource;
+import org.springframework.cache.interceptor.CacheOperation;
+
+/**
+ * getCacheOperations is called when beans are initialized and also from
+ * PageFragmentCachingFilter during requests; the filter needs annotations on
+ * controllers but if the standard lookup includes controllers, the return
+ * values from the controller method calls are cached unnecessarily.
+ *
+ * @author Burt Beckwith
+ */
+public class GrailsAnnotationCacheOperationSource extends AnnotationCacheOperationSource {
+
+ private static final long serialVersionUID = 1;
+
+ protected GrailsApplication application;
+
+ public Collection<CacheOperation> getCacheOperations(Method method, Class<?> targetClass,
+ boolean includeControllers) {
+
+ if (!includeControllers && isControllerClass(targetClass)) {
+ return null;
+ }
+
+ // will typically be called with includeControllers = true (i.e. from the filter)
+ // so controller methods will be considered
+ return super.getCacheOperations(method, targetClass);
+ }
+
+ @Override
+ public Collection<CacheOperation> getCacheOperations(Method method, Class<?> targetClass) {
+
+ // when called directly excluded controllers
+
+ if (isControllerClass(targetClass)) {
+ return null;
+ }
+
+ return super.getCacheOperations(method, targetClass);
+ }
+
+ protected boolean isControllerClass(Class<?> targetClass) {
+ return application.isArtefactOfType(ControllerArtefactHandler.TYPE, targetClass);
+ }
+
+ /**
+ * Dependency injection for the grails application.
+ * @param grailsApplication the app
+ */
+ public void setGrailsApplication(GrailsApplication grailsApplication) {
+ application = grailsApplication;
+ }
+}
View
10 src/java/grails/plugin/cache/web/filter/PageFragmentCachingFilter.java
@@ -14,6 +14,7 @@
*/
package grails.plugin.cache.web.filter;
+import grails.plugin.cache.GrailsAnnotationCacheOperationSource;
import grails.plugin.cache.SerializableByteArrayOutputStream;
import grails.plugin.cache.Timer;
import grails.plugin.cache.web.ContentCacheParameters;
@@ -57,7 +58,6 @@
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.interceptor.CacheEvictOperation;
import org.springframework.cache.interceptor.CacheOperation;
-import org.springframework.cache.interceptor.CacheOperationSource;
import org.springframework.cache.interceptor.CachePutOperation;
import org.springframework.cache.interceptor.CacheableOperation;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
@@ -154,7 +154,7 @@
}
}
- protected CacheOperationSource cacheOperationSource;
+ protected GrailsAnnotationCacheOperationSource cacheOperationSource;
protected final ThreadLocal<Stack<ContentCacheParameters>> contextHolder = new ThreadLocal<Stack<ContentCacheParameters>>() {
@Override
@@ -187,7 +187,7 @@ protected void doFilter(HttpServletRequest request, HttpServletResponse response
return;
}
Collection<CacheOperation> cacheOperations = cacheOperationSource.getCacheOperations(
- method, controllerClass);
+ method, controllerClass, true);
if (CollectionUtils.isEmpty(cacheOperations)) {
log.debug("No cacheable annotation found for {}:{} {}",
@@ -843,10 +843,10 @@ protected Object lookupController(Class<?> controllerClass) {
}
/**
- * Dependency injection for CacheOperationSource.
+ * Dependency injection for GrailsAnnotationCacheOperationSource.
* @param source
*/
- public void setCacheOperationSource(CacheOperationSource source) {
+ public void setCacheOperationSource(GrailsAnnotationCacheOperationSource source) {
cacheOperationSource = source;
}
Please sign in to comment.
Something went wrong with that request. Please try again.