Let's get started. I used the Spring 3.1.0.M1 release to build my application with Ehcache 2.4.2. You can use annotations with an older version of spring. Here is the link, http://code.google.com/p/ehcache-spring-annotations/.
Here is my ehcache.xml file:
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"
- updateCheck="false" monitoring="autodetect" dynamicConfig="true" name="Test">
- <diskStore path="java.io.tmpdir"/>
- <defaultCache maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- memoryStoreEvictionPolicy="LRU" />
- <cache name="Person"
- maxElementsInMemory="100"
- eternal="true"
- maxElementsOnDisk="1000"
- memoryStoreEvictionPolicy="LFU">
- </cache>
- </ehcache>
Call this ehcache.xml file from your Spring context file.
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
- xmlns:cache="http://www.springframework.org/schema/cache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/jdbc
- http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/cache
- http://www.springframework.org/schema/cache/spring-cache.xsd">
- <!-- scans the classpath for annotated components (including @Repostory
- and @Service that will be auto-registered as Spring beans -->
- <context:component-scan base-package="com.sample.ehcache" />
- <!-- Process cache annotations -->
- <cache:annotation-driven />
- <!-- Configuration for using Ehcache as the cache manager -->
- <bean
- id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
- p:cache-manager-ref="ehcache"/>
- <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
- p:config-location="classpath:ehcache.xml"/>
- </beans>
The power of the Spring context file is component-scan, annotation-driven and the Ehache entries. The component-scan tells Spring to scan these directories for beans that will be auto registered. Annotation driven turns on the annotations to use with Ehcache.
Next I create a base object that my data access object implement.
- public interface BaseDao {
- /**
- * Using the provided key return
- * the associated Object from the data
- * repository.
- * @param key
- * @return object having the associated key or
- * null if no associated Object is found with the provided
- * key
- */
- Object getObject(String key);
- /**
- * Clears all objects
- */
- void clearObjects();
- /**
- * clears object by key
- * @param key
- */
- void clearObject(String key);
- }
I implement the BaseDao in the following class and then add the @Cacheable and @CacheEvict annotations. It is just this simple.
- @Repository("baseDao")
- public class PersonDaoImpl implements BaseDao {
- private static final Logger LOGGER = Logger.getLogger(PersonDaoImpl.class.getName());
- /**
- * The @Cacheable automatically puts the data in the cache
- */
- @Cacheable("Person", key="#key")
- public final Person getObject(String key) {
- Person person = buildPerson(key);
- return person;
- }
- //stubbed data building
- private static Person buildPerson(String key) {
- LOGGER.debug("Building new Person object");
- Person person = new Person();
- person.setPersonId(key);
- person.setFirstName("Joe");
- person.setLastName("Smith");
- return person;
- }
- @CacheEvict(value="Person", allEntries=true)
- public void clearObjects() {
- LOGGER.debug("Person cache evicted....");
- }
- @CacheEvict(value="Person", key="#key")
- public void clearObject(String key) {
- LOGGER.debug("Person cache evicted....");
- }
- }
That is it! Now your data objects are cached on retrieval (doesn't matter where you get the data from) and your application performance will soar to new heights (and so will your status with your peers). Simple caching is just the beginning. There are loads of great features in Ehcache like Search, JTA, Write-Behind, and many more. Check out the Ehcache web site, www.ehcache.org, to get the full list of features.