Prior to Spring 3.1’s Cache Abstraction there was Ehcache Annotations for Spring (EAS). We had used the latter for quite a while without any problems, but I thought it would be better to switch to Spring’s own solution. Switching from one to the other was mostly trivial. There are minor differences in the annotations and bean configuration (to enable the aspect).

However, EAS’s configuration provides a create-missing-caches attribute to automatically create caches (based on the the configured 'default' cache). We depended on this heavily. In most cases, it seams silly to have to pre-defined a cache just so you can use it in an annotation.

Spring’s Cache Annotation did not provide an equavalent. However, based on this SO answer, I was able to easily simulate the EAS behavior by subclassing Spring’s EhCacheManager:

class LocalCacheManager extends EhCacheCacheManager {

    @Override
    public Cache getCache(String name) {
        Cache cache = super.getCache(name)
        if (cache == null) {
            log.debug("dynamically creating cache $name")
            getCacheManager().addCache(name)
            cache = new EhCacheCache(getCacheManager().getCache(name))
            addCache(cache)
        }
        return cache
    }
}

Wiring this in as the cache manager provides the same behavior as EAS’s create-missing-caches. Of course, you can still pre-define caches as usual if needed.