Grails Caching and EHCache Caching Controller Plugins, Not Maintenance Methods

I'm having trouble connecting the Grails EHCache module for caching service methods. Here's my setup:

Grails 2.4.2, JDK 1.7

BuildConfig.groovy compile ":cache-ehcache:1.0.4"

Config.groovy defines the next cache named "cache"

grails.cache.config = {
    cache {
        name 'cache'
        timeToLiveSeconds 60
    }
}

      

The following controller method is cached correctly. I tested by setting a breakpoint in the method and it only hits the first time until the cache expires.

@Cacheable('cache')
def index() {
    render "index-$params.id"
}

      

I can check the keys in the cache with:

grailsCacheManager.getCache('cache').getNativeCache().getKeys()

and see the value:

localhost:GET:/grails-cache-test/cache/index?id=34

So far so good.

There is the following service method:

@Cacheable('cache')
public method1(id) {
    "method1+$id"
}

      

Here I am setting a breakpoint and this method is always called regardless of the annotation @Cacheable

. I tried to set the annotation attributes value

and key

manually and there was no change. I am testing calling this method from a non-cached controller method as well as directly from a console plugin. When I get the keys in the service method, I see that there are no keys set.

I've looked at examples in the official documentation, code examples on the internet, github, the book I have, everything looks good, so I'm not sure where the problem is.

Any ideas as to why this service method is not caching the value? Thank!

UPDATE 1

I've been digging into the grails cache-1.1.8 plugin (as well as the associated cache-ehcache-1.0.4) and I think I found something useful. There PageFragmentCachingFilter.doFilter()

are calls to check controller annotations, but nothing to check. It looks like as a result, service annotations are never honored. There is a lot of documentation out there that mentions service methods, so I don't know if there is something elsewhere that handles this, or if this is a less common use case compared to controller methods.

UPDATE 2

It looks like controllers and services are being handled separately. In CacheAspectSupport.execute()

there is a loop for()

that will invoke cachePutRequest.apply(result.get());

, which will actually add the entry to the cache. Unfortunately, once a record is added to a request, it is not immediately available for retrieval. The main put()

code is part of Spring Source, so at this point I'm not sure if it's a Grails plugin issue or a Spring Source issue.

I created a JIRA issue for this plugin GPCACHEEHCACHE-16

+3


source to share





All Articles