From e79ce3bf4dbaa59075ee5316444fde7316fc4b5a Mon Sep 17 00:00:00 2001 From: Jose Solas Moreno Date: Mon, 23 Jan 2023 14:53:11 -0300 Subject: [PATCH] Ignore caching in controllers currently when caching a method in a controller the parameter is ignored and returns the same result for all calls --- .gitignore | 1 + .../com/demo/DemoController.groovy | 7 ++++++ grails-app/views/demo/show.gsp | 1 + .../compiler/CacheableTransformation.groovy | 7 ++++++ ...NotCachingControllerIntegrationSpec.groovy | 22 +++++++++++++++++++ 5 files changed, 38 insertions(+) create mode 100644 grails-app/views/demo/show.gsp create mode 100644 src/integration-test/groovy/com/demo/NotCachingControllerIntegrationSpec.groovy 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..16867341 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) { + [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 d275a344..72a1b2ba 100644 --- a/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy +++ b/src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy @@ -53,6 +53,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' + } +}