0

i have some standard Spring @Service classes in a separate jar lib that use the standard @Cacheable Spring annotation, in a Spring boot project i declare the dependency, configure a CacheManager and just works!

i try to do the same in a Grails 3.1 project but with no luck! i discover that https://github.com/grails-plugins/grails-cache require to use its 'proprietary' @Cacheable annotation:

http://grails-plugins.github.io/grails-cache/3.0.x/api/grails/plugin/cache/Cacheable.html

As workaround i FORKED some Service just to use the Grails @Cacheable and it's working but i'd like to have a single @Service that works under grails or not!


I have misconfigured something, it doesn't behave the same way, but i can't figure out what is it!

i'd like to share this jar lib between spring only & grails projects, but so far i can't make caching work, unless i forked the service calsses under grails-app/services directory & used 'proprietary' @Cacheable annotation;

i try to remove the plugin and configure the bean and the cache in: /GPsGrails3/grails-app/init/gpsgrails3/Application.groovy with the org.springframework.context.annotation.Bean annotation:

@Primary
@Bean
public ConcurrentMapCacheManager concurrentMapCacheManager() {
    return new ConcurrentMapCacheManager();
}

@Bean
public SignatureService signatureService() {
    SignatureService _signatureService = new SignatureService();
    return _signatureService;
}

i put signatureService under grails-app/services directory: /GPsGrails3/grails-app/services/it/finmatica/ifw/impl/SignatureService.groovy

Maybe i have to configure my beans in: /GPsGrails3/grails-app/conf/spring/resources.groovy ?

i have to use the version 4 of the plugin? compile "org.grails.plugins:cache:4.+"

1 Answers1

0

I am not sure what the question is but you don't have to use the Grails @Cacheable annotation. If you want to use the Spring one in a Grails app, you can, and it will behave in the same way that it would if you weren't using Grails. You don't even need the plugin in order to do that.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47
  • 1
    When you use the Grails annotation, that triggers an AST Transformation which adds the Spring annotation in the byte code so that Spring's runtime proxy generation mechanism will recognize it and will create the proxy that does the caching. – Jeff Scott Brown Sep 23 '16 at 18:13
  • 1
    That will change in version 4 of the `cache` plugin, which is rigging up all of the caching at compile time and will not require a runtime proxy. You could still use the Spring annotation if you wanted to, but there won't be many good reasons to do that for services written in Groovy. – Jeff Scott Brown Sep 23 '16 at 18:14
  • Except when you need to evict upon multiple caches – Tjad Clark Jul 07 '20 at 03:55
  • "Except when you need to evict upon multiple caches" - @TjadClark I am not sure which claim that is supposed to be an exception to. What does the "Except" follow? Thanks for clarification! – Jeff Scott Brown Jul 07 '20 at 15:36
  • Grails annotations do not handle complex caching appropriately. You can't use multiple annotations of the same type, which limits flexibility https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/Caching.html – Tjad Clark Jul 08 '20 at 15:12
  • thanks for responding. That's been my only pitfall with caching plugin. It would also be cool if it supported spring annotations out of the box. I currently turn off grails caching due to conflict. Would be cool if both spring/grail interfaces were wired to same cache manager instance. – Tjad Clark Jul 08 '20 at 15:19
  • And this sucks, because I lose all the benefits of the compile time cache annotations that grails provide. – Tjad Clark Jul 08 '20 at 15:19
  • "It would also be cool if it supported spring annotations out of the box." - It does. – Jeff Scott Brown Jul 08 '20 at 17:09
  • That was not my experience. Perhaps I'm not articulating well enough. If I set the cacheManager to "GrailsConcurrentLinkedMapCacheManager". The spring annotations don't work, unless I @EnableCaching . When I do that, there is a conflict of cache managers. This is my problem. – Tjad Clark Jul 10 '20 at 07:40
  • "your ignorance will only keep your plugin behind" - I am sorry that I couldn't help. All the best! – Jeff Scott Brown Jul 10 '20 at 13:23
  • You may think I have a bad attitude, I'm just observant, I don't have a bias. I was the one who upvoted your comments, And it was I who was trying to help your community with my very best. – Tjad Clark Jul 11 '20 at 12:02