diff --git a/.gitignore b/.gitignore index 736ad6a9..048d9733 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,4 @@ grails-app/conf/BootStrap.groovy grails-app/conf/spring/ grails-app/views/error.gsp grails-app/views/index.gsp +bin/ diff --git a/grails-app/controllers/com/demo/DemoController.groovy b/grails-app/controllers/com/demo/DemoController.groovy index 7933a349..6a6e53d0 100644 --- a/grails-app/controllers/com/demo/DemoController.groovy +++ b/grails-app/controllers/com/demo/DemoController.groovy @@ -1,10 +1,17 @@ package com.demo +import grails.plugin.cache.Cacheable + class DemoController { def basicCachingService def grailsCacheAdminService + @Cacheable('show') + def show (params) { + return [param: params.id] + } + def clearBlocksCache() { grailsCacheAdminService.clearBlocksCache() render "cleared blocks cache" diff --git a/grails-app/views/demo/show.gsp b/grails-app/views/demo/show.gsp new file mode 100644 index 00000000..f101f793 --- /dev/null +++ b/grails-app/views/demo/show.gsp @@ -0,0 +1 @@ +Hello World!${param} \ No newline at end of file diff --git a/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy b/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy index ba369659..24efa363 100644 --- a/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy +++ b/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy @@ -30,6 +30,7 @@ import org.codehaus.groovy.control.CompilePhase import org.codehaus.groovy.control.SourceUnit import org.codehaus.groovy.transform.GroovyASTTransformation import org.springframework.cache.Cache +import org.springframework.context.ApplicationContext import static org.codehaus.groovy.ast.ClassHelper.* import static org.grails.datastore.gorm.transform.AstMethodDispatchUtils.* @@ -55,6 +56,13 @@ class CacheableTransformation extends AbstractCacheTransformation { @Override protected Expression buildDelegatingMethodCall(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, MethodNode methodNode, MethodCallExpression originalMethodCallExpr, BlockStatement newMethodBody) { + boolean isControllerClass = classNode.name.endsWith('Controller') + boolean isServiceClass = classNode.name.endsWith('Service') + + if (isControllerClass && !isServiceClass) { + return originalMethodCallExpr + } + VariableExpression cacheManagerVariableExpression = varX(GRAILS_CACHE_MANAGER_PROPERTY_NAME) handleCacheCondition(sourceUnit, annotationNode, methodNode, originalMethodCallExpr, newMethodBody) diff --git a/src/integration-test/groovy/com/demo/NotCachingControllerIntegrationSpec.groovy b/src/integration-test/groovy/com/demo/NotCachingControllerIntegrationSpec.groovy new file mode 100644 index 00000000..b572c5d3 --- /dev/null +++ b/src/integration-test/groovy/com/demo/NotCachingControllerIntegrationSpec.groovy @@ -0,0 +1,22 @@ +package com.demo + +import geb.spock.GebSpec +import grails.testing.mixin.integration.Integration + +@Integration +class NotCachingControllerIntegrationSpec extends GebSpec { + + void 'test action controller with different parameters'() { + when: + go '/demo/show/1' + + then: + $().text().contains 'Hello World!1' + + when: + go '/demo/show/2' + + then: + $().text().contains 'Hello World!2' + } +}