ciaranw edited this page Jan 4, 2011 · 7 revisions
Clone this wiki locally

Tapestry Service Cache is an add-on library for Tapestry 5 that makes caching of services simple. It uses annotations to mark methods as cacheable and takes care of all the heavy lifting for you.

How To Use

Tapestry Service Cache has a few requirements for services it can cache:

  • The service to cache must implement an interface - this is so Tapestry can generate a proxy to the service implementation and allow the service to be decorated.
  • The method(s) you would like to cache should have 0 or 1 method parameter(s). If you have one parameter to your method, the value of that method is used as a discriminator in the key of the cached value.

You enable caching by putting a @CacheResult annotation on the implementation method. This will cache the result of the method invocation in a cache with the same name as the id of the service. The @CacheResult annotation can also take a parameter, cacheName. This value will be used as the name of the cache in which the values are stored, instead of the service id. If you would like to use the parameter of the method as part of the cache key, put @CacheKey on the parameter.

  import com.ciaranwood.tapestry.cache.services.annotations.CacheKey;
  import com.ciaranwood.tapestry.cache.services.annotations.CacheResult;
  public class CachedService implements Service {

    public String expensiveOperation() {
      //some expensive operation here

    public String expensiveOperation(@CacheKey Integer key) {
      //This uses the key parameter as a discriminator in the cache key, so
      //invocations with different parameters are cached under different keys.

Frequently Asked Questions

** Q. My method is not being cached, but I added @CacheResult to the method I want to cache! **

A. Most likely, you have placed the annotation on the interface. This is not supported (@CacheResult must go on the implementation method, not the interface). If the service does not have an interface, you must add one, as Tapestry does not support advice around services that do not have an interface.

Also, you may have put the annotation on a method that is not part of the service interface. Tapestry always creates a proxy when the service implements an interface. This is how we add the caching functionality, so therefore we cannot add this without the Tapestry proxy.

** Q. How can I set custom settings for my cache? **

A. By default, tapestry-service-cache uses the defaultCache settings from the ehcache-default.xml file found in the library's jar file. You can use your own custom ehcache.xml file however. Simply contribute to the ApplicationDefaults service in your Module class using this key: com.ciaranwood.tapestry.cache.ehcache-configuration-file. You can use the ServiceCacheConstants.EHCACHE_CONFIGURATION_FILE static constant. By default, tapestry-service-cache will use the service id as the cache name, unless an explicit cacheName is provided with the @CacheResult annotation.