Published: 2010-04-14 · Java & Web Architecture Archive

Caching Java Methods with Spring 3 and Ehcache

Declarative method caching with Spring 3 and Ehcache removes repetitive boilerplate from service layers while shielding relational databases from hot read queries.

Configuration Overview

<ehcache:annotation-driven cache-manager="ehCacheManager" />

@Cacheable(cacheName = "catalogItems")
public Item findByCode(String code) {
    return repository.load(code);
}

Always configure bounded maxElementsInMemory and explicit TTL policies so stale objects cannot exhaust JVM heap space. Related: Using Spring in modular enterprise apps and JVM GC pause tuning.