Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aspect is executed every time on @Cacheable method #58

Open
gabivis opened this issue Jan 14, 2019 · 3 comments
Open

Aspect is executed every time on @Cacheable method #58

gabivis opened this issue Jan 14, 2019 · 3 comments

Comments

@gabivis
Copy link

gabivis commented Jan 14, 2019

Consider a method annotated with grails.plugin.cache.Cacheable.
testgsp.PanelsRefresher

@Cacheable(value="gabiTest") public Integer gabiTest() { log.info("Gabi is testing") return 5 }
Consider an aspect that should be executed when gabiTest method is executed:

`@Aspect
@slf4j
class TestAspect {

@Around("execution(* testgsp.PanelsRefresher.gabi*())")
public Object gabiAspect(ProceedingJoinPoint jp) {
    def result
    log.info("Aspect for gabi before.")
    try {
        result = jp.proceed()
    } catch (Exception e) {
        log.info("Exception execution method ${jp.getSignature().getName()}",e)
        result = jp.proceed()
    }
    log.info("Aspect for gabi after.")
    return result
}`

The aspect will be executed every time, even if the result is cached.

Sample output when executing PanelsRefresher.gabiTest() multiple times:

2019-01-14 15:20:15.151 INFO --- [ main] testgsp.PanelsRefresher : Gabi is testing 2019-01-14 15:20:15.151 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after. 2019-01-14 15:20:15.152 INFO --- [ main] testgsp.TestAspect : Aspect for gabi before. 2019-01-14 15:20:15.155 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after. 2019-01-14 15:20:15.155 INFO --- [ main] testgsp.TestAspect : Aspect for gabi before. 2019-01-14 15:20:15.155 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after. 2019-01-14 15:20:15.155 INFO --- [ main] testgsp.TestAspect : Aspect for gabi before. 2019-01-14 15:20:15.155 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after. 2019-01-14 15:20:15.156 INFO --- [ main] testgsp.TestAspect : Aspect for gabi before. 2019-01-14 15:20:15.156 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after. 2019-01-14 15:20:15.156 INFO --- [ main] testgsp.TestAspect : Aspect for gabi before. 2019-01-14 15:20:15.156 INFO --- [ main] testgsp.TestAspect : Aspect for gabi after.

This is not reproducible when using the spring Cacheable annotation, together with @EnableCaching annotation on Application.groovy.

I am using grails 3.3.9 with grails cache plugin 4.0.1. In grails 2.5.2, with cache:1.1.8 this works fine.

@graemerocher
Copy link
Member

This is not fixable since the newer cache plugin does not use Spring AOP/proxying and instead directly modifies the byte code of the target class through an AST transformation, which is better for performance and debugging, but means it cannot run before dynamic Spring AOP aspects

@gabivis
Copy link
Author

gabivis commented Jan 14, 2019

So if i want the behaviour I had before, I have to use the spring annotation, but I won't benefit from the performance enhancements available for grails cache plugin annotations?

@graemerocher
Copy link
Member

that is correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants