In a module of my application, a complex query is very slow. It needs about 5 seconds because the application gets data from nearly 10 tables. Most of the 10 tables are not updated often. Therefor I decided to use cache for the 10 tables.
Software: Spring+hibernate
ehcache is the default cache for hibernate. So I choosen it.

Step1.? Modify ehcache.xml in /webroot/web-inf/classes. the content is below:
??? ?? ?? ??
?????????? <ehcache>
?? ??? ??? ? ?? <diskStore path="java.io.tmpdir" />
??? ?? ? ?? ??? <defaultCache maxElementsInMemory="10000" eternal="false"
??? ?? ??? ? ?? ??? overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120"
??? ?? ??? ? ?? ??? diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
??? ??? ??? </ehcache>


Step2. Configure applicationContext-hibernate.xml in the spring framework.
???????????? .....
? ? ? ? ? ? <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> ??????
??????? ?? ? ??? .....
???? ??? ??? ??? ??? <prop key="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
??? ??????????????? <prop key="hibernate.cache.use_query_cache">true</prop>
??? ?? ? ?????????? <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>

???????????????? ...
??????????? </bean>

Step3. Configure the hibernate mapping file(ie. *.hbm.xml), add a new attribute:
??????????? <cache usage="read-only"/>

Step 4. Change the dao. Before it retireves data, it will be asked to use cache, for example:
??? ? ? ? ? ?????? getHibernateTemplate().setCacheQueries(true);
?????????????? or
?????????????????? query.setCacheable(true)

Now, everything looks very good. It only need less than 1 second to get all the data. Much faster than before.

??????????