Skip to content

Commit

Permalink
Ignore caching in controllers
Browse files Browse the repository at this point in the history
currently when caching a method in a controller the parameter is ignored and returns the same result for all calls
  • Loading branch information
jsolas committed Jun 22, 2024
1 parent be4a972 commit e79ce3b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ grails-app/conf/BootStrap.groovy
grails-app/conf/spring/
grails-app/views/error.gsp
grails-app/views/index.gsp
bin/
7 changes: 7 additions & 0 deletions grails-app/controllers/com/demo/DemoController.groovy
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
1 change: 1 addition & 0 deletions grails-app/views/demo/show.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World!${param}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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'
}
}

0 comments on commit e79ce3b

Please sign in to comment.