Spring cache @Cacheable method ignored when called from within the same class

前端 未结 3 1036
无人共我
无人共我 2020-11-27 04:08

I\'m trying to call a @Cacheable method from within the same class:

@Cacheable(value = \"defaultCache\", key = \"#id\")
public Person findPerson         


        
3条回答
  •  余生分开走
    2020-11-27 04:31

    For anyone using the Grails Spring Cache plugin, a workaround is described in the documentation. I had this issue on a grails app, but unfortunately the accepted answer seems to be unusable for Grails. The solution is ugly, IMHO, but it works.

    The example code demonstrates it well:

    class ExampleService {
        def grailsApplication
    
        def nonCachedMethod() {
            grailsApplication.mainContext.exampleService.cachedMethod()
        }
    
        @Cacheable('cachedMethodCache')
        def cachedMethod() {
            // do some expensive stuff
        }
    }
    

    Simply replace exampleService.cachedMethod() with your own service and method.

提交回复
热议问题