Skip to content

Commit 5b8bd40

Browse files
committed
Ignore caching in controllers
currently when caching a method in a controller the parameter is ignored and returns the same result for all calls
1 parent 384428a commit 5b8bd40

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ grails-app/conf/BootStrap.groovy
2525
grails-app/conf/spring/
2626
grails-app/views/error.gsp
2727
grails-app/views/index.gsp
28+
bin/

grails-app/controllers/com/demo/DemoController.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.demo
22

3+
import grails.plugin.cache.Cacheable
4+
35
class DemoController {
46

57
def basicCachingService
68
def grailsCacheAdminService
79

10+
@Cacheable('show')
11+
def show (params) {
12+
return [param: params.id]
13+
}
14+
815
def clearBlocksCache() {
916
grailsCacheAdminService.clearBlocksCache()
1017
render "cleared blocks cache"

grails-app/views/demo/show.gsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World!${param}

src/ast/groovy/org/grails/plugin/cache/compiler/CacheableTransformation.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.codehaus.groovy.control.CompilePhase
3030
import org.codehaus.groovy.control.SourceUnit
3131
import org.codehaus.groovy.transform.GroovyASTTransformation
3232
import org.springframework.cache.Cache
33+
import org.springframework.context.ApplicationContext
3334

3435
import static org.codehaus.groovy.ast.ClassHelper.*
3536
import static org.grails.datastore.gorm.transform.AstMethodDispatchUtils.*
@@ -55,6 +56,13 @@ class CacheableTransformation extends AbstractCacheTransformation {
5556
@Override
5657
protected Expression buildDelegatingMethodCall(SourceUnit sourceUnit, AnnotationNode annotationNode, ClassNode classNode, MethodNode methodNode, MethodCallExpression originalMethodCallExpr, BlockStatement newMethodBody) {
5758

59+
boolean isControllerClass = classNode.name.endsWith('Controller')
60+
boolean isServiceClass = classNode.name.endsWith('Service')
61+
62+
if (isControllerClass && !isServiceClass) {
63+
return originalMethodCallExpr
64+
}
65+
5866
VariableExpression cacheManagerVariableExpression = varX(GRAILS_CACHE_MANAGER_PROPERTY_NAME)
5967
handleCacheCondition(sourceUnit, annotationNode, methodNode, originalMethodCallExpr, newMethodBody)
6068

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.demo
2+
3+
import geb.spock.GebSpec
4+
import grails.testing.mixin.integration.Integration
5+
6+
@Integration
7+
class NotCachingControllerIntegrationSpec extends GebSpec {
8+
9+
void 'test action controller with different parameters'() {
10+
when:
11+
go '/demo/show/1'
12+
13+
then:
14+
$().text().contains 'Hello World!1'
15+
16+
when:
17+
go '/demo/show/2'
18+
19+
then:
20+
$().text().contains 'Hello World!2'
21+
}
22+
}

0 commit comments

Comments
 (0)