Fork me on GitHub

5 Mar 2010

Page fragment caching with Grails Springcache plugin

Yesterday I released the new version of the Springcache plugin for Grails. The new feature this brings to the table is page fragment caching driven by annotations on controller actions. The feature is based on EhCache Web and is even simpler and more powerful than the full page caching I blogged about recently. With the page fragment caching feature you can:

  • Define @Cacheable and @CacheFlush annotations on controller actions.
  • Have SiteMesh decorate cached and uncached output alike.
  • Use SiteMesh to mix dynamic page sections with cached sections.
  • Use SiteMesh and <g:include> to have multiple areas of the page that are cached and flushed independently of one another.
  • Cache the output of controller actions that use content negotiation separately according to the requested format.

There are some examples in the plugin documentation and the plugin source code has a test project with a variety of page fragment caching mechanisms used and tested.

7 comments:

Anonymous said...

Robert--is this plugin compatible with 1.3.2?
I've installed the springcache plugin per the documentation but am unable to compile an annotated controller. Am using 1.3.2

In Config.groovy:

springcache {
defaults {
eternal = false
diskPersistent = false
}
caches {
menuControllerCache {
memoryStoreEvictionPolicy = "LRU"
}
}
}

MenuController.groovy:

package myPackage

class MenuController extends BaseController {

@Cacheable("menuControllerCache")
def myCachedClosure = {
// do something
}
}

from the terminal:

Running script /usr/local/grails/scripts/Compile.groovy
Environment set to development
[mkdir] Created dir: /Users/shawntho/.grails/1.3.2/projects/CMS1/plugin-classes
[groovyc] Compiling 35 source files to /Users/shawntho/.grails/1.3.2/projects/CMS1/plugin-classes
[groovyc] /Users/shawntho/Web/workspace/CMS1/grails-app/controllers/edu/washington/cev/cms1/MenuController.groovy: 5: unable to resolve class Cacheable , unable to find class for annotation
[groovyc] @ line 5, column 5.
[groovyc] @Cacheable("menuControllerCache")
[groovyc] ^
[groovyc]
[groovyc] 1 error
Compilation error: Compilation Failed

The plugin classes are where they should be at:
~/.grails/1.3.2/projects/CMS1/plugin-classes/plugin/springcache

Any ideas?

-Shawn

Rob said...

There's every chance this is caused by this bug which affects Grails 1.3.2 and 1.3.3. Does the same thing happen if you use Grails 1.3.1?

Anonymous said...

Thanks Robert. I downgraded to 1.2.2 and it compiled. Couldn't really test it though as other aspects of my app need a newer Grails version. Any workaround suggestions?

Rob said...

actually looking again, I'm not able to reproduce this with a 1.3.3 app. I'll try to reproduce it with 1.3.2 tomorrow. If you are able to can you JIRA the issue and attach a sample project that reproduces the problem? That would really help me get to the bottom of it.

Anonymous said...

I've created a JIRA for the problem:

https://cvs.codehaus.org/browse/GRAILS-6498

-Shawn

Rob said...

Your controller isn't importing the annotation class. The full class name is grails.plugin.springcache.annotations.Cacheable

Anonymous said...

Thanks for looking at the JIRA Robert. I had tried importing the package directly but realize now that I'd made a typo (import grails.plugins.springcache.annotations.*). I also thought the methods were dynamically added at runtime.

-Shawn