Query Caching ( Using Ehcache )
You can also enable query caching. To do so configure it in your hbm.xml:
property key = "hibernate.cache.use_query_cache" >true</ property
|
sessionFactory.getCurrentSession().createQuery( "..." ).setCacheable( true ).list(); |
< property name = "net.sf.ehcache.configurationResourceName" >/ehcache.xml</ property > |
< cache name = "com.somecompany.someproject.domain.Country" maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "300" timeToLiveSeconds = "600" overflowToDisk = "true" /> |
Please note that in ehcache.xml, if eternal=”true” then we should not write timeToIdealSeconds, timeToLiveSeconds, hibernate will take care about those values
So if you want to give values manually better use eternal=”false” always, so that we can assign values into timeToIdealSeconds, timeToLiveSeconds manually.
timeToIdealSeconds=”seconds” means, if the object in the global cache is ideal, means not using by any other class or object then it will be waited for some time we specified and deleted from the global cache if time is exceeds more than timeToIdealSeconds value.
timeToLiveSeconds=”seconds” means, the other Session or class using this object or not, i mean may be it is using by other sessions or may not, what ever the situation might be, once it competed the time specified timeToLiveSeconds, then it will be removed from the global cache by hibernate.
Second level caching ( Using Ehcache )
A very beautiful example for Understanding Ehcache and Implementing it is here explainedSecond level caching using Ehcache. - By Pankaj